In this example, you will learn about C++ program to check leap year in two different ways.
A year that is exactly divisible by four is a leap year, except for years that are divisible by 100, but these century years are leap years if they are exactly divisible by 400.
//Easy C++ program to check for leap year
#include <iostream>
using namespace std;
int main ()
{
int year;
cout << "Enter a year you want to check: ";
cin >> year;
//leap year condition
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if(year % 400 == 0)
cout << year << " is a leap year." << endl;
else
cout << year << " is not a leap year." << endl;
}
else
cout << year << " is a leap year." << endl;
}
else
cout << year << " is not a leap year." << endl;
return 0;
}
Output
//C++ program for leap year in single line
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter a year you want to check: ";
cin >> year;
//leap year condition
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
cout << year << " is a leap year." << endl;
else
cout << year << " is not a leap year." << endl;
return 0;
}
Output
Enter a year you want to check: 1990 1990 is not a leap year. Enter a year you want to check: 2000 2000 is a leap year.