#include // In the Gregorian calendar, a year is a leap year if // // 1. it is divisible by 4, // // AND // // 2. either // // a. it is not divisible by 100, // // OR // // b. it is divisible by 400. // // Thus, // // 1996 is a leap year, // 1900 is not a leap year, // 2000 is a leap year, and // 2025 is not a leap year. // // Write a program that asks the user for a positive // integer representing a year. // // If the integer is zero or negative, print // // The year must be positive. // // Otherwise, indicate whether or not the year is a // leap year. // Function declarations int main(); // Function definitions int main() { int year{}; std::cout << "Enter a year: "; std::cin >> year; return 0; } // Hint: // // You can determine whether an integer is divisible by // another integer using the remainder operator. // // For example, 'year' is divisible by 4 precisely when // // year%4 == 0. // // Similarly, // // year%100 != 0 // // means that 'year' is not divisible by 100. // // Try translating the leap-year rule directly: // // divisible by 4 // // AND // // (not divisible by 100 OR divisible by 400). // // You will need both the && and || logical operators if // you want to express the entire leap-year rule using a // single logical condition. // The resulting condition is // // (year%4 == 0) // && ((year%100 != 0) || (year%400 == 0)) // // Test your condition using years such as: // // year leap year? // // 1996 yes // 1997 no // 1900 no // 2000 yes // 2100 no // 2400 yes // // In particular, 1900 and 2000 are useful tests because // they distinguish the complete rule from simply checking // divisibility by 4. // Here is one solution: /* int main() { int year{}; std::cout << "Enter a year: "; std::cin >> year; if ( year <= 0 ) { std::cout << "The year must be positive." << std::endl; } else if ( (year%4 == 0) && ((year%100 != 0) || (year%400 == 0)) ) { std::cout << year << " is a leap year." << std::endl; } else { std::cout << year << " is not a leap year." << std::endl; } return 0; } */ // We can also implement the same rule using nested // conditional statements rather than combining the // conditions with && and ||. // // Here is one such solution: /* int main() { int year{}; std::cout << "Enter a year: "; std::cin >> year; if ( year <= 0 ) { std::cout << "The year must be positive." << std::endl; return 0; } bool is_leap_year{}; if ( year%4 == 0 ) { if ( year%100 == 0 ) { if ( year%400 == 0 ) { is_leap_year = true; } } else { is_leap_year = true; } } if ( is_leap_year ) { std::cout << year << " is a leap year." << std::endl; } else { std::cout << year << " is not a leap year." << std::endl; } return 0; } */ // // Both solutions implement the same rule. // // Which do you find easier to read? // // The first solution more directly expresses the // mathematical condition: // // divisible by 4 // // AND // // (not divisible by 100 OR divisible by 400). // // The second solution instead follows the decision process // one test at a time. It uses simpler individual conditions, // but requires more levels of nesting.