/************************************************ * leap year -- a program that calculates * * whether or not a given year is a leap * * year * * * * Author: Dan Emmons * * * * Purpose: This was written in order to * * complete Exercise 6-4 in the book * * "Practical C++ Programming" by Steve * * Oualline * ************************************************/ #include #include int year; // the year provided by the user float year_divided4; // the year divided by 4 float year_divided100; // the year divided by 100 float year_divided400; // the year divided by 400 int int_divided4; // year_divided4 converted to an integer int int_divided100; // year_divided100 converted to an integer int int_divided400; // year_divided400 converted to an integer float check4; // check for divisibility by 4 float check100; // check for divisibility by 100 float check400; // check for divisibility by 400 int main() { std::cout << "Enter a year to see if it is a leap year or enter 0 (zero) to exit: "; std::cin >> year; if(year <= 0){ std::cout << "You must enter a positive number: "; std::cin >> year; } if(year > 30000){ std::cout << "The year is too far into the future.\n"; std::cout << "Please enter another year: "; std::cin >> year; } while(year != 0){ year_divided4 = year / 4.00; year_divided100 = year / 100.00; year_divided400 = year / 400.00; int_divided4 = (year / 4); int_divided100 = (year / 100); int_divided400 = (year / 400); check4 = year_divided4 - int_divided4; check100 = year_divided100 - int_divided100; check400 = year_divided400 - int_divided400; assert(check4 >= 0); assert(check4 < 1); assert(check100 >= 0); assert(check100 < 1); assert(check400 >= 0); assert(check400 < 1); if(check4 != 0) std::cout << '\n' << year << " is not a leap year.\n\n"; if((check4 == 0) && (check100 != 0)) std::cout << '\n' << year << " is a leap year.\n\n"; if((check4 == 0) && (check100 == 0)){ if(check400 == 0) std::cout << '\n' << year << " is a leap year.\n\n"; if(check400 != 0) std::cout << '\n' << year << " is not a leap year.\n\n"; } std::cout << "Enter another year or enter 0 (zero) to exit: "; std::cin >> year; } return (0); }