• Support PF! Buy your school textbooks, materials and every day products Here!

C++ Leap Year Function

  • Comp Sci
  • Thread starter White Ink
  • Start date
19
0
1. Homework Statement

Write a function that prompts the user to enter a year and that returns a 1 if the entered year is a leap year, and a 0 otherwise.

A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.

3. The Attempt at a Solution

I have done this much and when I test my program, it seems to return a value of 1 no matter what year I enter. I'm pretty awful at programming and I was wondering if anyone could spot where my mistake is.


#include <iostream>

using namespace std;

bool leap(int) {
int n;
bool b;

if (n%4==0) { b=true; } // if n is divisible by 4, it is a leap year
else if (n%100==0) {
if (n%400==0) { b=true; }
else { b=false; }
} // if n is divisible by 100, check if it is divisible by 400, if it is, it is a leap year
else { b=false; } // if n is not divisible by 4 (=> it is not divisible by 400 either), then it is not a leap year

return b;
}

int main () {
int n;
cout << "Please enter a year: " << '\n';
cin >> n;

cout << leap(n);

}
 

Answers and Replies

D H
Staff Emeritus
Science Advisor
Insights Author
15,329
681
A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.
Wrong definition, so you get the wrong answer.

A year is a leap year if it is divisible by 4 and it either is not divisible by 100 or it is divisible by 400.
 
HallsofIvy
Science Advisor
Homework Helper
41,734
893
1. Homework Statement

Write a function that prompts the user to enter a year and that returns a 1 if the entered year is a leap year, and a 0 otherwise.

A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.

3. The Attempt at a Solution

I have done this much and when I test my program, it seems to return a value of 1 no matter what year I enter. I'm pretty awful at programming and I was wondering if anyone could spot where my mistake is.


#include <iostream>

using namespace std;

bool leap(int) {
int n;
bool b;

if (n%4==0) { b=true; } // if n is divisible by 4, it is a leap year
else if (n%100==0) {

This will only be accessed if n is not divisible by 4 which, if n is divisible by 100, is impossible.
if (n%400==0) { b=true; }
else { b=false; }
This says, if n is divisible by 400, b= true. Under any other condition, and it doesn't matter what happend if the first "if", b will be equal to false. In other words, if n is any number that isnot a multiple of 400, you return "false".
} // if n is divisible by 100, check if it is divisible by 400, if it is, it is a leap year
else { b=false; } // if n is not divisible by 4 (=> it is not divisible by 400 either), then it is not a leap year

return b;
}

int main () {
int n;
cout << "Please enter a year: " << '\n';
cin >> n;

cout << leap(n);

}
What you have written will return "true" if n is divisible by 400, otherwise it will return "false".
 
chroot
Staff Emeritus
Science Advisor
Gold Member
10,165
34
Be careful with your "else" statements. You are using at least one "else" statement in an unintended way.

- Warren
 
109
0
is this close to the solution?

using namespace std;
int main () {
//declare variable integer for year, y;
int y, r1, r2, r3;
//ask user to input a year;
cout << "Enter a year and press ENTER " << endl;
//place year in int y;
cin >> y;
//divide year by 4 and find remainder;
r1 = y % 4;
r2 = y % 400;
r3= y % 100;
//main if statement: if rem of y/4 is 0 divide by 400, if there is no rem pri\
nt y is not leap year and end prg;
if (r1==0) {r2 = y % 400;
// if rem of y/400 is 0 then print y is leap year and end prg, otherwise divi\
de y/100;
if (r2==0) {cout << y << " is a leap year." << endl;
//if rem of y/100=0 cout y is not leap year, end prg, otherwise print y is \
leap year, end prg;
else if (r3==0) {cout << y << " is NOT a leap year." << endl;
else cout << y << " is a leap year" <<endl;}}}
else cout <<y<< " is NOT a leap year." << endl;
return 0;
}
 

Related Threads for: C++ Leap Year Function

  • Last Post
Replies
2
Views
18K
Replies
3
Views
840
  • Last Post
Replies
1
Views
1K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
2
Views
6K
Replies
19
Views
9K
Top