Write a C++ program to check that year is leap year or not?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <iostream> using namespace std; int main() { int year; int leapYearCheck; cout << "Enter a year: "; cin >> year; // Determine if the year is a leap year if (year % 400 == 0) { leapYearCheck = 1; // Leap year } else if (year % 100 == 0) { leapYearCheck = 2; // Not a leap year } else if (year % 4 == 0) { leapYearCheck = 1; // Leap year } else { leapYearCheck = 2; // Not a leap year } // Output the result switch (leapYearCheck) { case 1: cout << year << " is a Leap Year.\n"; break; case 2: cout << year << " is not a Leap Year.\n"; break; default: cout << "Invalid year.\n"; break; } return 0; } |
Possible Outputs
Output #1
Enter a year: 2000
2000 is a Leap Year.
Output #2
Enter a year: 1900
1900 is not a Leap Year.
Output #3
Enter a year: 2024
2024 is a Leap Year.
Output #4
Enter a year: 2023
2023 is not a Leap Year.