Beginner · boolean logic

Leap Year Check

Read a year and decide if it's a leap year. The rule sounds simple — "divisible by 4" — and then the centuries show up. A perfect exercise in &&, || and reading requirements carefully.

At a glancePrint "leap year" when the year is divisible by 4 but not by 100, or divisible by 400 — 1900 is not a leap year, 2000 is, and both are in the hidden tests.

Problem

Input: one integer year (1 ≤ year ≤ 3000).

Output: leap year or not a leap year (exactly these words).

The rule: a year is leap if it's divisible by 4 and not by 100, or it's divisible by 400.

Samples:

InputOutputWhy
2024leap yeardivisible by 4, not by 100
1900not a leap yeardivisible by 100 but not by 400
2000leap yeardivisible by 400

Your solution

main.cpp — your solution
Custom input for "Run my input"

Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.

Stuck? Hints

Hint 1 — translate the sentence directly

"divisible by 4 and not by 100, or divisible by 400" becomes (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0). Parentheses matter: && binds tighter than ||, but explicit parens make the intent readable.

Hint 2 — the tests that catch lazy solutions

A "divisible by 4" only solution passes 2024 but fails 1900 (should be no) and 2000 (should be yes). The judge includes exactly those trap years — that's what edge-case testing means.

Solution (try it first!)
#include <iostream>
using namespace std;

int main() {
    int y;
    cin >> y;
    bool leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
    if (leap) cout << "leap year" << endl;
    else cout << "not a leap year" << endl;
    return 0;
}

Note cout prints bool as 1/0 — that's why we branch on it instead of printing it directly. Conditionals and booleans are everywhere; the Maximum of Three problem drills the same muscle differently.

Deep dive

Why this works

The rule is a chain of exceptions — divisible by 4, except centuries, except every 400th year. It is a test of reading a spec literally instead of writing what sounds right.

The cost

O(1) — at most three modulo operations.

Where it goes wrong

  • year % 4 == 0 alone says 1900 is a leap year. It is not: 1900 % 100 == 0 and 1900 % 400 != 0.
  • a || b && c groups as a || (b && c) — && binds tighter. Missing parentheses here is the classic bug.

Next step

More edge cases to get exactly right: Maximum of Three.

Two things to remember

  • 1900 is not a leap year; 2000 is.
  • && binds tighter than || — parenthesise or be bitten.
🐞 This version fails — open it and try

Only tests % 4 — 1900 slips through

#include <iostream>
using namespace std;

int main() {
    int y;
    cin >> y;
    if (y % 4 == 0) cout << "leap year" << endl;
    else cout << "not a leap year" << endl;
    return 0;
}