C++ Program to Check Leap Year
In this example, we will learn program that checks whether an year (integer) entered by the user is a leap year or not.
To understand this example, you should have the knowledge of the following C++ programming topics:
Leap Year
All years which are perfectly divisible by 4 are leap years except for century years (years ending with 00) which is leap year only it is perfectly divisible by 400.
For example: 2012, 2004, 1968 etc are leap year but, 1971, 2006 etc are not leap year. Similarly, 1200, 1600, 2000, 2400 are leap years but, 1700, 1800, 1900 etc are not.
In this program below, user is asked to enter a year and this program checks whether the year entered by user is leap year or not.
Example 1: Program to Check if a year is leap year or not using only if-else statement.
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter a year: ";
cin >> year;
if (((year % 4 == 0) && (year % 100 != 0)|| (year % 400 == 0)))
{
cout << year << " is a leap year.";
}
else
{
cout << year << " is not a leap year.";
}
return 0;
}
Output 1
Enter a year: 2020 2010 is a leap year.
Output 2
Enter a year: 2018 2010 is not a leap year.
Example 2: Program to Check if a year is leap year or not using nested if-else statement
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter a year: ";
cin >> year;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
return 0;
}
Output 1
Enter a year: 2010 2010 is not a leap year.
Output 2
Enter a year: 2012 2012 is a leap year.
Next Example
We hope that this Example helped you develop better understanding of the concept of "Find Leap Year" in C++.
Keep Learning : )
In the next Example, we will learn about C++ Find Factorial
.