- #1
White Ink
- 19
- 0
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.
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);
}