Write a C++ Program to Check Leap Year

In this tutorial, we’ll discuss how to write a C++ program to check whether a year is a leap year or not.

A leap year is a year that has an extra day, February 29th, which is added to keep the calendar year synchronized with the astronomical or seasonal year.

To determine whether a year is a leap year or not, we need to follow these rules:

  • If the year is divisible by 4, it could be a leap year.
  • If the year is divisible by 100, it might not be a leap year.
  • If the year is divisible by 400, it is definitely a leap year.

With these rules in mind, we can write a simple C++ program to check whether a given year is a leap year or not. Here’s the program:

#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." << 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;
}

First, we declare an integer variable called year. We then prompt the user to enter a year by using the cout and cin statements.

Next, we use an if statement to check whether the year is divisible by 4. If it is, we move on to the next if statement to check if it’s divisible by 100.

If it is, we move on to the last if statement to check if it’s divisible by 400. If it is, we output a message saying that the year is a leap year.

If it’s not, we output a message saying that it’s not a leap year.

If the year is not divisible by 100, we simply output a message saying that it’s a leap year.

If the year is not divisible by 4, we output a message saying that it’s not a leap year.

And that’s it! This simple program will determine whether a given year is a leap year or not.