Given a year, check whether the given year is leap year or not
The astronomical problem:
What happens without leap years?
The solution: Add an extra day (February 29) every 4 years!
But wait... it's actually 365.2425 days, not exactly 365.25!
That tiny difference of 0.0075 days requires the 100-year and 400-year rules.
The Gregorian Calendar Rules (established 1582):
Rule 1: Divisible by 4 โ LEAP YEAR
Rule 2: BUT if divisible by 100 โ NOT a leap year
Rule 3: EXCEPT if divisible by 400 โ IS a leap year
Complete algorithm in order:
โ ๏ธ CRITICAL CONCEPT: Order determines correctness!
Why check 400 FIRST?
Consider year 2000:
โ WRONG ORDER (checking 4 first):
if (year % 4 == 0) leap // Would return TRUE here for 2000
else if (year % 100 == 0) not leap // Never checked!
else if (year % 400 == 0) leap // Never reached!
Result: 2000 is leap year โ (accidentally correct, but logic is flawed)
โ WRONG ORDER (checking 100 before 400):
if (year % 4 == 0) leap
else if (year % 100 == 0) not leap // Returns FALSE here for 2000!
else if (year % 400 == 0) leap // Never reached!
Result: 2000 is NOT leap year โ WRONG!
โ CORRECT ORDER (400 โ 100 โ 4):
if (year % 400 == 0) leap // Catches 2000 correctly!
else if (year % 100 == 0) not leap // Skipped for 2000
else if (year % 4 == 0) leap // Skipped for 2000
Result: 2000 is leap year โ CORRECT!
The principle: Check MOST SPECIFIC rule first!
๐ก This is like exception handling: check special cases before general cases!
Our one-line condition:
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
Breaking it down:
Part 1: year % 400 == 0
Part 2: (year % 100 != 0 && year % 4 == 0)
Why it works:
Year | %400 | %100 | %4 | Part 1 | Part 2 | Result |
---|---|---|---|---|---|---|
2000 | 0 | 0 | 0 | TRUE | - | LEAP |
1900 | 300 | 0 | 0 | FALSE | FALSE | NOT LEAP |
2012 | 12 | 12 | 0 | FALSE | TRUE | LEAP |
2013 | 13 | 13 | 1 | FALSE | FALSE | NOT LEAP |
The logic flow:
Regular leap years (divisible by 4, not by 100):
Year | %400 | %100 | %4 | Result |
---|---|---|---|---|
2004 | 4 | 4 | 0 | Leap year โ |
2012 | 12 | 12 | 0 | Leap year โ |
2024 | 24 | 24 | 0 | Leap year โ |
Century years (divisible by 100, test the exception):
Year | %400 | %100 | %4 | Result |
---|---|---|---|---|
1700 | 100 | 0 | 0 | NOT leap year โ |
1800 | 200 | 0 | 0 | NOT leap year โ |
1900 | 300 | 0 | 0 | NOT leap year โ |
2100 | 100 | 0 | 0 | NOT leap year โ |
Special 400-year cases (exception to the exception):
Year | %400 | %100 | %4 | Result |
---|---|---|---|---|
1600 | 0 | 0 | 0 | Leap year โ |
2000 | 0 | 0 | 0 | Leap year โ |
2400 | 0 | 0 | 0 | Leap year โ |
Non-leap years (not divisible by 4):
Year | %4 | Result |
---|---|---|
2013 | 1 | NOT leap year โ |
2014 | 2 | NOT leap year โ |
2015 | 3 | NOT leap year โ |
Example 1: year = 2012 (regular leap year)
year = 2012
2012 % 400 == 0
(2012 % 100 != 0 && 2012 % 4 == 0)
Example 2: year = 1900 (century, not leap)
year = 1900
1900 % 400 == 0
(1900 % 100 != 0 && 1900 % 4 == 0)
else
Example 3: year = 2000 (400-year exception)
year = 2000
2000 % 400 == 0
Example 4: year = 2013 (not divisible by 4)
year = 2013
2013 % 400 == 0
โ FALSE โ(2013 % 100 != 0 && 2013 % 4 == 0)
Method 1: Our Compact Solution
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
cout << "Leap year";
else
cout << "Not a Leap year";
โ
Compact and efficient
โ
Handles all cases correctly
โ Can be hard to understand at first
Method 2: Nested If-Else (Explicit Priority)
if (year % 400 == 0) {
cout << "Leap year";
} else if (year % 100 == 0) {
cout << "Not a Leap year";
} else if (year % 4 == 0) {
cout << "Leap year";
} else {
cout << "Not a Leap year";
}
โ
Very clear and readable
โ
Shows priority explicitly
โ
Easy to understand logic flow
โ More lines of code
Method 3: Boolean Variable
bool isLeap = (year % 400 == 0) ||
(year % 100 != 0 && year % 4 == 0);
if (isLeap)
cout << "Leap year";
else
cout << "Not a Leap year";
โ
Separates logic from output
โ
Reusable boolean value
โ
Clear variable name explains purpose
๐ก All three methods are correct! Choose based on readability preference.
Where leap year logic is used:
1. Calendar Applications:
2. Financial Systems:
3. Age Calculation:
4. Event Planning:
5. Date Validation:
6. Programming Languages:
calendar.isleap()
Year.isLeap()
๐ก Fun fact: People born on Feb 29 are called "leaplings" or "leapers" and technically only have birthdays every 4 years!
๐ก Key Takeaway: Always check most specific conditions first (400 before 100 before 4). This pattern applies to many programming problems with hierarchical rules!
#include <iostream>
using namespace std;
int main() {
int year;
cin >> year;
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
cout << "Leap year";
else
cout << "Not a Leap year";
return 0;
}