#include <iostream>
using namespace std;
int main() {
int what_year;
cout << "Enter calendar year ";
cin >> what_year;
if (what_year - (n * 4) = 0 ) {
cout << "leap year";
}
else
{
cout << "wont work";
}
system("Pause");
return 0;
}
Trying to make a program for class, to find a leap year.. not sure how to ask C++ if an integer is divisible by a number?
The leap year rule is
if year modulo 400 is 0 then
is_leap_year
else if year modulo 100 is 0 then
not_leap_year
else if year modulo 4 is 0 then
is_leap_year
else
not_leap_year
http://en.wikipedia.org/wiki/Leap_year#Algorithm
You can use the modulo operator to see if one number is evenly divisible by another, that is if there is no remainder from the division.
2000 % 400 = 0 // Evenly divisible by 400
2001 % 400 = 1 // Not evenly divisible by 400
Interestingly, several prominent software implementations did not apply the "400" part, which caused February 29, 2000 not to exist for those systems.
Use the modulo function.
if ((year % 4) == 0)
{
//leap year
}
Note that this does not account for the 100 and 400 year jump.
Proper code would be something like
if(((year%4) == 0) && (((year%100)!=0) || ((year%400) == 0))
{
//leap year
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With