- Rule
- quadrennial, not a century exception
Check Leap Year in C++
What you’ll learn
- The Gregorian leap rule: divisible by 4, except most centuries, unless divisible by 400.
- A compact
is_leap_yearpredicate andint main()style from the reference. - Printing every leap year in 2024–2050 (matching the reference output) plus a live preview.
Overview
Leap years keep the civil calendar aligned with the tropical year by inserting an extra day (February 29). The decision rule is pure integer modular arithmetic—ideal for a short if chain in C++.
Two programs
2024 single check and 2024–2050 listing.
Live preview
Integer years in a safe range; same predicate as the C++ samples.
Rigor
Century vs quadricentennial cases and adoption caveats in the FAQ.
Prerequisites
The modulo operator %, boolean && and ||, and for loops.
#include <iostream>,int main(),std::cout.- Operator precedence:
%binds tighter than==; parentheses keep the intent obvious.
What is a leap year?
In the Gregorian calendar, a year is a leap year if February has 29 days. The arithmetic rule fixes the drift of a pure 365-day year.
Every year divisible by 4 is a leap candidate; century years (% 100 == 0) are ruled out unless they are divisible by 400.
Predicate
Let L(y) be true iff (y mod 4 = 0 ∧ y mod 100 ≠ 0) ∨ (y mod 400 = 0). Then L(y) is the standard proleptic Gregorian leap-year predicate.
20242024 mod 4 = 0 and 2024 mod 100 = 24 ≠ 0, so L(2024) holds.
Intuition
- Rule
2021 mod 4 = 1
Takeaway: divisibility by 4 is necessary but not sufficient once centuries enter the picture.
Live preview
Integer years in a reasonable safe range (this widget uses |y| ≤ 1000000).
Algorithm
Goal: evaluate the Gregorian leap predicate for an integer year.
Quadrennial test
Check year % 4 == 0. If false, the year is common.
Century refinement
If year % 100 == 0, require year % 400 == 0 to remain leap; otherwise the century exception applies.
📜 Pseudocode
function is_leap_year(y):
return (y mod 4 = 0 and y mod 100 != 0) or (y mod 400 = 0) Single year: 2024
Same condition as the reference (isLeapYear), renamed is_leap_year, with int main().
#include <iostream>
int is_leap_year(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
int year = 2024;
if (is_leap_year(year)) {
std::cout << year << " is a leap year.\n";
} else {
std::cout << year << " is not a leap year.\n";
}
return 0;
} Explanation
Short-circuiting && and || match the usual truth table; parentheses mirror the prose rule.
Leap years in [2024, 2050]
Matches the reference bounds and output line (the old page text incorrectly mentioned 2000–2030 in one bullet). Adds a trailing newline after the spaced years.
#include <iostream>
int is_leap_year(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
}
return 0;
}
int main() {
int start_year = 2024;
int end_year = 2050;
std::cout << "Leap years in the range " << start_year << " to " << end_year << ":\n";
for (int y = start_year; y <= end_year; ++y) {
if (is_leap_year(y)) {
std::cout << y << " ";
}
}
std::cout << "\n";
return 0;
} Explanation
Leap years spaced by 4 until a century boundary; 2100 would break the pattern, but this interval stops at 2050.
Alternatives
Lookup tables. For a fixed window (like a UI calendar), precompute leap flags once.
Date libraries. Production systems often delegate to C++ date/time facilities (for example <chrono>) or platform APIs after validating components.
Interview: recite the 4 / 100 / 400 rule cleanly; mention real-world adoption only if asked.
❓ FAQ
🔄 Input / output examples
Change year in Example 1 or start_year / end_year in Example 2.
| Year | Leap? |
|---|---|
2024 | Yes |
2021 | No |
2000 | Yes |
1900 | No |
Edge cases and pitfalls
The predicate is only part of a full date library; validating month/day and time zones is separate work.
Quadricentennial leap
2400 will be leap; 2200 will not.
Astronomical year 0
Do not mix astronomical 0 with historical 1 BC without a clear convention.
start > end
Swap bounds or guard the loop to avoid empty or inverted scans.
int width
Typical int is plenty for civil years; use wider types only if you embed years in packed timestamps.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| One year | O(1) | O(1) |
Range [a, b] | O(b - a + 1) | O(1) |
No auxiliary memory is required beyond loop indices.
Summary
- Rule:
(y%4==0 && y%100!=0) || (y%400==0). - Code: one boolean function plus a simple range loop for listings.
- Watch-outs: Gregorian adoption history, negative years with
%in C++, and consistent range prose.
The Julian calendar had a leap every four years without the century exception. The Gregorian reform (1582 in several countries) dropped 10 days and refined the rule so years like 1900 are common years while 2000 stays a leap year.
8 people found this page helpful
