Join our newsletter for the latest updates.

C++ Program to Check Leap Year

This program 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:


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: Check if a year is leap year or not

#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

Enter a year: 2014
2014 is not a leap year.

Here, we have used nested if statements to check whether the year given by the user is a leap year or not.

First, we check if year is divisible by 4 or not. If it is not divisible, then it is not a leap year.

If it is divisible by 4, then we use an inner if statement to check whether year is divisible by 100.

If it is not divisible by 100, it is still divisible by 4 and so it is a leap year.

We know that the century years are not leap years unless they are divisible by 400.

So, if year is divisible by 100, another inner if statement checks whether it is divisible by 400 or not.

Depending on the result of that innermost if statement, the program determines whether year is a leap year or not.

Did you find this article helpful?