#include<iostream>
int main()
{
std::cout << "Enter an year: ";
int year = 0;
std::cin >> year;
// An year is leap if:
// - BOTH can be evenly divide by 4
// - AND if can be evenly divided by 100, can be divided by 400 TOO
if (year % 4 == 0) {
if(year % 100 != 0) { // it can't be divided by 100, but it can by 4: it's leap
std::cout << "This is a leap year\n";
} elseif(year % 400 == 0) {
std::cout << "This is a leap year\n";
} else {
std::cout << "This is not a leap year\n";
}
} else {
std::cout << "This is not a leap year\n";
}
// A more 'compressed' logic is:
// An year is leap if:
// - EITHER can be evenly divide by 4 AND can NOT be divided by 100
// - OR it can be divided by 400
if( (0 == year % 4 && 0 != year % 100) || (0 == year % 400) ) {
std::cout << "This is a leap year\n";
} else {
std::cout << "This is not a leap year\n";
}
return 0;
}
#include <iostream>
usingnamespace std;
bool isLeap( int year ) { return !( year % 4 ) && ( year % 100 || !( year % 400 ) ); }
int main()
{
int year;
cout << "Enter year: "; cin >> year;
cout << year << ( isLeap( year ) ? " is " : " is not " ) << "a leap year";
}