Skip to content
C++ Better Explained
Go back
Leap Year Program in C++: Check if a Year Is a Leap Year

Leap Year Program in C++

Checking whether a year is a leap year is a classic beginner exercise, and it’s a great way to practice the modulo operator and boolean logic. The rules seem fiddly at first, but they collapse into a single clean condition.


The Leap Year Rule

A leap year has 366 days instead of 365. The rule has three parts:

  1. A year divisible by 4 is a leap year, except
  2. a year divisible by 100 is not a leap year, unless
  3. it is also divisible by 400, in which case it is a leap year.

So 2024 is a leap year (divisible by 4), 1900 is not (divisible by 100 but not 400), and 2000 is (divisible by 400). We test “divisible by” with the modulo operator %, which gives the remainder: year % 4 == 0 means “divides evenly by 4.”


The Straightforward Version with if/else

Let’s translate the three rules directly into nested if statements so you can see the logic step by step:

#include <iostream>

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

    bool leap;
    if (year % 4 != 0) {
        leap = false;                 // not divisible by 4 -> common year
    } else if (year % 100 != 0) {
        leap = true;                  // divisible by 4 but not 100 -> leap
    } else if (year % 400 != 0) {
        leap = false;                 // divisible by 100 but not 400 -> common
    } else {
        leap = true;                  // divisible by 400 -> leap
    }

    if (leap)
        std::cout << year << " is a leap year.\n";
    else
        std::cout << year << " is not a leap year.\n";
    return 0;
}

Reading it top to bottom mirrors the rules exactly, which makes it easy to trust.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

The One-Line Condition

Once you understand the rules, you can express them as a single boolean expression. A year is a leap year when it’s divisible by 4 and not by 100, or it’s divisible by 400:

#include <iostream>

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

    bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

    std::cout << year << (leap ? " is" : " is not") << " a leap year.\n";
    return 0;
}

The parentheses matter. The left side (year % 4 == 0 && year % 100 != 0) catches ordinary leap years like 2024. The right side (year % 400 == 0) rescues century years like 2000. Because they’re joined by ||, either one being true makes the whole thing true.


A Reusable isLeapYear Function

If you need this check in more than one place, wrap it in a function that returns bool. This keeps your logic in one spot and reads almost like English:

#include <iostream>

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

int main() {
    int years[] = {1900, 2000, 2024, 2025};
    for (int y : years) {
        std::cout << y << ": " << (isLeapYear(y) ? "leap" : "common") << "\n";
    }
    return 0;
}

Output:

1900: common
2000: leap
2024: leap
2025: common

Wrapping the logic in isLeapYear is efficient because you write and test the rule once, then reuse it anywhere without copying the condition around.


Common Mistakes

The most frequent bug is checking only year % 4 == 0 and forgetting the century exceptions — that wrongly labels 1900 a leap year. The second is getting the && / || grouping wrong; without the parentheses, operator precedence can change the meaning. When in doubt, test the tricky cases: 1900 (common), 2000 (leap), and any year divisible by 4 but not 100 (leap).



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
GCD and LCM in C++: The Euclidean Algorithm Explained
Next Post
Reverse a Number in C++: Flip the Digits with a While Loop

Keep Learning