/******************************************************************************************** leapyear.cpp Jim Millard for CO211/Fall 1999 Simple program that tells the user if a given year is or is not a leap year ********************************************************************************************/ #include <iostream> #include <string> using namespace std; int main() { int year; //inform and prompt the user cout << endl << "This program will indicate if a year is or is not a leap year." << endl; cout << endl << "Enter a year: "; cin >> year; cout << endl; /* now we do our comparisons and output the results. the rules are: 1) if a year is before 1584, it cannot be a leap year, as leapyears were not used prior to that year. 2) if a year is NOT divisible by 4, it cannot be a leap year 3) if the year IS divisible by 100 (therefore, it IS divisible by 4), but is NOT divisible by 400, it cannot be a leap year Note that the rules as written above make it easier to find a year that is NOT a leap year, so that's the primary mode of logic we're using. */ if (1584 > year) //too early to be a leap year cout << year << " is NOT a leap year. Leap years did not occur prior to 1584." << endl; else if (0 != year%4) //not divisible by 4, so it's not a leap year. cout << year << " is NOT a leap year." << endl; else if ((0 == year%100) && (0 != year%400)) //divisible by 100, but not by 400 cout << year << " is NOT a leap year." << endl; else cout << year << " IS a leap year." << endl; /* although it's messier, the entire logic can be formulated as a single IF. Unless you have a GOOD reason to do it, ORed statements should be done through chained if-else-if statements instead: if ((1584 > year) || (0 != year%4) || ((0 == year%100) && (0 != year%400))) cout << year << " is NOT a leap year." << endl; else cout << year << " IS a leap year." << endl; You can also reverse the logic to get the IS part first: if ( (1584 <= year) && ( ((0 == year%4) && (0 != year%100)) || (0 == year%400) ) ) cout << year << " IS a leap year." << endl; else cout << year << " is NOT a leap year." << endl; */ return 0; }