C++ Program to Check Leap Year



A leap year contains one additional day, i.e february 29, to keep the calendar year synchronized with the earth's orbit around the sun. We will write a C++ program that checks whether a given year is a leap year or not.

A year that is divisible by 4 is known as a leap year. However, years divisible by 100 are not leap years while those divisible by 400 are.

Here is a list of some leap years and non-leap years:

Leap Year Non-Leap Year
2016 2017
2020 2018
1600 1700
2000 2023
2400 1900
We can check whether a year is a leap year in C++ using different methods. Below are the common approaches:

Checking Leap Year Using if-else Statements

In this approach, we use if-else statements which are used to check conditions. It's the most common way to find out if a year is a leap year or not.

Example

In the program below, we first check if the year is divisible by 4. If it is, we then check if it's divisible by 100, because years like 1700 or 1800 can be divisible by 4 but aren't leap years. So, we specifically check century years, and if the year is divisible by 100, we make sure it's also divisible by 400.

#include <iostream>
using namespace std;

// Function to check if the given year is a leap year
void checkLeapYear(int givenYear) {
    cout << "Checking if "<< givenYear<<" is a leap year " << endl;

    if (givenYear % 4 == 0) {
        if (givenYear % 100 == 0) {
            if (givenYear % 400 == 0)
                cout << "Output: " << givenYear<< " is a leap year" << endl;
            else
                cout << "Output: " <<givenYear << " is not a leap year" << endl;
        } else {
            cout << "Output: " << givenYear<< " is a leap year" << endl;
        }
    } else {
        cout << "Output: " <<givenYear << " is not a leap year" << endl;
    }
}

int main() {
    int sampleYear = 2000; // You can change this to test other years
    checkLeapYear(sampleYear);
    return 0;
}

The output below shows the result of checking if a year is a leap year using the if-else statement.

Checking if 2000 is a leap year 
Output: 2000 is a leap year

Time Complexity: O(1), It checks a few conditions only, so it runs instantly.

Space Complexity: O(1), It uses just one variable (year), no extra space needed.

Checking Leap Year Using Ternary Operator

In this approach, we use the ternary operator represented by ? and :, which allows us to check a condition in a single line. It's a shorthand way of writing an if-else statement.

Example

In the program below, the ternary operator checks if the year is divisible by 4, and for century years, also by 400. If both the condition is true, it prints that the year is a leap year. Otherwise, it prints that it is not a leap year.

#include <iostream>
using namespace std;

int main() {
    int year = 2000;  // You can change this to test other years
    cout << "Checking if " << year << " is a leap year" << endl;

    (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 
        cout << "Output: " << year << " is a leap year "<< endl : 
        cout << "Output: " << year << " is not a leap year " << endl;

    cout << "=== Code Execution Successful ===" << endl;
    return 0;
}

Below shows the output of the above program, which displays whether the year 2000 is a leap year.

Checking if 2000 is a leap year
Output: 2000 is a leap year 

Time Complexity: O(1) because the modulus and logical operations are constant time.

Space Complexity: O(1) because only a fixed amount of space is used.

Checking Leap Year by Creating User-defined Function

In this approach, we create a separate function to check the leap year logic and call it from the main function. A function is just a block of code designed to perform a specific task.

Example

Below is the complete C++ program where we pass the year to a function. The function returns true if it's a leap year, and false if it's not.

#include <iostream>
using namespace std;

// Function to check if the given year is a leap year
bool isLeap(int year) {
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}

int main() {
    int year = 2000;  // You can change this to test other years
    cout << "Checking if " << year << " is a leap year"<< endl;

    if (isLeap(year))
        cout << "Output: " << year << " is a leap year"<< endl;
    else
        cout << "Output: " << year << " is not a leap year"<< endl;
    return 0;
}

Once we run the above program, we will get the following output showing whether a year is a leap year or not.

Checking if 2000 is a leap year
Output: 2000 is a leap year

Time Complexity: O(1) because function and condition checks take constant time.

Space Complexity: O(1) because only a few variables are used.

Checking Leap Year Using Logical Expression

In this approach, we use a boolean variable to check if a year is a leap year. If it's true, the year is a leap year; if it's false, it's not. A boolean holds either true or false and helps in checking conditions.

Example

Here's a complete C++ program where we use a logical expression to check if a year is a leap year.

#include <iostream>
using namespace std;

int main() {
    int checkYear = 2000;  // You can change this value
    cout << "Checking if " << checkYear << " is a leap year" << endl;

    bool isLeapYear = (checkYear % 4 == 0 && (checkYear % 100 != 0 || checkYear % 400 == 0));

    if (isLeapYear)
        cout << "Output: " << checkYear << " is a leap year"<< endl;
    else
        cout << "Output: " << checkYear << " is not a leap year" << endl;
    return 0;
}

The output below shows the result for checking the leap year using the logical expression.

Checking if 2000 is a leap year
Output: 2000 is a leap year

Time Complexity: O(1).

Space Complexity: O(1).

Updated on: 2025-05-13T17:07:56+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements