0
0
CppProgramBeginner · 2 min read

C++ Program to Check Leap Year with Output and Explanation

A C++ program to check leap year uses the condition if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) to print if the year is leap or not.
📋

Examples

Input2000
Output2000 is a leap year.
Input1900
Output1900 is not a leap year.
Input2024
Output2024 is a leap year.
🧠

How to Think About It

To check if a year is leap, first see if it divides evenly by 400. If yes, it is leap. If not, check if it divides evenly by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
📐

Algorithm

1
Get the input year from the user.
2
Check if the year is divisible by 400; if yes, it is a leap year.
3
Else, check if the year is divisible by 4 but not by 100; if yes, it is a leap year.
4
Otherwise, it is not a leap year.
5
Print the result.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int year;
    cout << "Enter a year: ";
    cin >> year;

    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
        cout << year << " is a leap year." << endl;
    } else {
        cout << year << " is not a leap year." << endl;
    }
    return 0;
}
Output
Enter a year: 2024 2024 is a leap year.
🔍

Dry Run

Let's trace the year 2024 through the code to see how it checks for leap year.

1

Input year

year = 2024

2

Check divisibility by 400

2024 % 400 = 24 (not zero), so condition false

3

Check divisibility by 4 and not by 100

2024 % 4 = 0 (true) and 2024 % 100 = 24 (not zero), so condition true

4

Print result

Print '2024 is a leap year.'

CheckCondition Result
year % 400 == 0false
year % 4 == 0 && year % 100 != 0true
💡

Why This Works

Step 1: Divisible by 400

If a year divides evenly by 400, it is always a leap year, so we check year % 400 == 0 first.

Step 2: Divisible by 4 but not 100

If not divisible by 400, a year must be divisible by 4 but not by 100 to be leap, checked by year % 4 == 0 && year % 100 != 0.

Step 3: Otherwise not leap

If neither condition is true, the year is not a leap year.

🔄

Alternative Approaches

Using nested if-else
cpp
#include <iostream>
using namespace std;

int main() {
    int year;
    cout << "Enter a year: ";
    cin >> year;

    if (year % 400 == 0) {
        cout << year << " is a leap year." << endl;
    } else {
        if (year % 100 == 0) {
            cout << year << " is not a leap year." << endl;
        } else if (year % 4 == 0) {
            cout << year << " is a leap year." << endl;
        } else {
            cout << year << " is not a leap year." << endl;
        }
    }
    return 0;
}
This approach uses nested if-else statements for clarity but is longer.
Using a function to check leap year
cpp
#include <iostream>
using namespace std;

bool isLeapYear(int year) {
    return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}

int main() {
    int year;
    cout << "Enter a year: ";
    cin >> year;

    if (isLeapYear(year)) {
        cout << year << " is a leap year." << endl;
    } else {
        cout << year << " is not a leap year." << endl;
    }
    return 0;
}
This approach separates logic into a function for reuse and better structure.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic and logical operations regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store the year and results, so space complexity is constant O(1).

Which Approach is Fastest?

All approaches run in constant time, but using a function improves code readability without affecting performance.

ApproachTimeSpaceBest For
Single if conditionO(1)O(1)Simple and concise code
Nested if-elseO(1)O(1)Clear step-by-step logic
Function-based checkO(1)O(1)Reusable and organized code
💡
Remember to check divisibility by 400 first because it overrides the other rules.
⚠️
Beginners often forget to exclude years divisible by 100 but not by 400, causing wrong leap year results.