C++ Program to Check Leap Year

In this post, we will learn how to check leap year using C++ Programming language.

A Leap year has 366 days (the extra day is the 29th February).

To check whether a year is leap year or not, there are following parameters which we have to check:

  • Exactly divisible by 4
    • Not Exactly divisible by 100
    • Except if it is exactly divisible by 400

Hence, we have to check whether the year is exactly divisible by 4 and 400 and not divisible by 100 to be a leap year.

So, without further ado, let’s begin this tutorial.

C++ Program to Check Leap Year

// C++ Program to Check Leap Year
#include <iostream>
using namespace std;

int main(){
    int year;
    
    // Asking for input
    cout << "Enter the year: ";
    cin >> year;
    
    // Checking Leap 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 the year: 2028
2028 is a leap year.

Output 2

Enter the year: 2050
2050 is not a leap year
.

How Does This Program Work ?

    int year;

In this program, we have declared an int data type variable named year.

    // Asking for input
    cout << "Enter the year: ";
    cin >> year;

Then, the user is asked to enter the year.

    // Checking Leap 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";

Now, we check whether the entered year is exactly divisible by 4 and 400 and not divisible by 100. If all of the conditions are true, then the entered year is a leap year otherwise it’s not a leap year.

C++ Program to Check Leap Year in One Line

// C++ Program to Check Leap Year
#include <iostream>
using namespace std;

int main(){
    int year;
    
    // Asking for input
    cout << "Enter the 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 the year: 2012
2012 is a leap year.

Output 2

Enter the year: 2012
2012 is a leap year.

Conclusion

I hope after going through this post, you understand how to check whether a year is a leap year or not using C++ Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *