C++ Program to Check Leap Year using If-else Statement
ADVERTISEMENTS
C++ program to check leap year using if-else statement. In this article, you will learn how to make a c++ program to check leap year using if-else statement.
Sample of the Leap Years
2000, 2004, ....., 2016, 2020 are the leap years
C++ Program to Check Leap Year using If-else Statement
// C++ Program to Check Leap Year using If-else Statement
#include <iostream>
using namespace std;
int main() {
int y; // Value of the year
cout << "----Enter the year----\n";
cin >> y;
if (y % 400 == 0) {
cout << "\n" << y << " is the leap year.\n";
} else if (y % 100 == 0) {
cout << "\n" << y << " is not the leap year.\n";
} else if (y % 4 == 0) {
cout << "\n" << y << " is the leap year.\n";
} else {
cout << "\n" << y << " is not the leap year.\n";
}
return 0;
}
Output
----Enter the year----
1997
1997 is not the leap year.
C++ Program to Check Leap Year using Switch Case Statement
// C++ Program to Check Leap Year using Switch Case Statement
#include <iostream>
using namespace std;
int main() {
int y, r;
// y - Value of the year
cout << "----Enter the year----\n";
cin >> y;
r = y % 400 == 0 || y % 100 == 0 || y % 4 == 0;
switch (r) {
case 1:
cout << "\n" << y << " is the leap year.\n";
break;
case 0:
cout << "\n" << y << " is not the leap year.\n";
break;
default:
cout << "\n" << y << " is not the leap year.\n";
}
return 0;
}
Output
----Enter the year----
2004
2004 is the leap year.