11. Check Leap Year

Easy โฑ๏ธ 12 min

Problem Statement

Given a year, check whether the given year is leap year or not

Input Format:
Enter an integer as a input
Output Format:
Print the year is "Leap year" or "Not a Leap year"
Constraints:
1 <= INPUT <=10^15
Sample Input 1:
2012
Sample Output 1:
Leap year
Sample Input 2:
1900
Sample Output 2:
Not a Leap year
๐Ÿ“… The Leap Year Rule - Why 400, 100, and 4?
๐ŸŒ Why Do Leap Years Exist? โ–ผ

The astronomical problem:

  • Earth takes 365.25 days (approximately) to orbit the Sun
  • Our calendar has only 365 days
  • Difference: 0.25 days (6 hours) per year

What happens without leap years?

  • After 4 years: We're behind by 1 full day
  • After 100 years: We're behind by 25 days
  • Seasons would drift: Summer would eventually occur in winter months!

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 Complete Leap Year Algorithm โ–ผ

The Gregorian Calendar Rules (established 1582):

Rule 1: Divisible by 4 โ†’ LEAP YEAR

  • Examples: 2004, 2008, 2012, 2016, 2020, 2024
  • Adds back the missing ~0.25 day per year

Rule 2: BUT if divisible by 100 โ†’ NOT a leap year

  • Examples: 1700, 1800, 1900, 2100, 2200
  • Why? Because 365.25 is slightly too much!
  • We overcorrect by ~0.0075 days per year
  • After 100 years, we're ahead by ~0.75 days
  • So skip 3 out of every 4 century years

Rule 3: EXCEPT if divisible by 400 โ†’ IS a leap year

  • Examples: 1600, 2000, 2400
  • Fine-tunes the correction
  • Keeps calendar accurate over centuries

Complete algorithm in order:

  1. If year % 400 == 0 โ†’ LEAP YEAR โœ… (overrides everything)
  2. Else if year % 100 == 0 โ†’ NOT leap year โŒ
  3. Else if year % 4 == 0 โ†’ LEAP YEAR โœ…
  4. Else โ†’ NOT leap year โŒ
๐ŸŽฏ Why Priority Order Matters: 400 โ†’ 100 โ†’ 4 โ–ผ

โš ๏ธ CRITICAL CONCEPT: Order determines correctness!

Why check 400 FIRST?

Consider year 2000:

  • 2000 % 4 == 0 โœ… (divisible by 4)
  • 2000 % 100 == 0 โœ… (divisible by 100)
  • 2000 % 400 == 0 โœ… (divisible by 400)

โŒ 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!

  • 400 is most specific (only every 400 years)
  • 100 is less specific (every 100 years)
  • 4 is least specific (every 4 years)

๐Ÿ’ก This is like exception handling: check special cases before general cases!

๐Ÿงฎ Our Solution: Compact Condition Explained โ–ผ

Our one-line condition:

if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))

Breaking it down:

Part 1: year % 400 == 0

  • Divisible by 400? โ†’ Definitely a leap year!
  • Examples: 1600, 2000, 2400
  • If TRUE, entire condition is TRUE (due to OR)

Part 2: (year % 100 != 0 && year % 4 == 0)

  • NOT divisible by 100 AND divisible by 4
  • This catches regular leap years (4, 8, 12... but NOT 100, 200, 300)
  • Examples: 2004, 2008, 2012, 2016, 2020, 2024

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:

  1. Check if divisible by 400 first (most specific)
  2. If not, check if it's divisible by 4 but NOT by 100
  3. This automatically handles all three rules in correct order!
๐Ÿ“Š Testing Critical Years โ–ผ

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 โŒ
๐Ÿ” Step-by-Step Execution Examples โ–ผ

Example 1: year = 2012 (regular leap year)

  1. Read: year = 2012
  2. Check: 2012 % 400 == 0
    • 2012 รท 400 = 5 remainder 12
    • FALSE โŒ
  3. Check: (2012 % 100 != 0 && 2012 % 4 == 0)
    • 2012 % 100 = 12 (not 0) โ†’ TRUE โœ…
    • 2012 % 4 = 0 โ†’ TRUE โœ…
    • TRUE && TRUE โ†’ TRUE โœ…
  4. Overall: FALSE || TRUE โ†’ TRUE
  5. Output: "Leap year"

Example 2: year = 1900 (century, not leap)

  1. Read: year = 1900
  2. Check: 1900 % 400 == 0
    • 1900 รท 400 = 4 remainder 300
    • FALSE โŒ
  3. Check: (1900 % 100 != 0 && 1900 % 4 == 0)
    • 1900 % 100 = 0 (IS 0!) โ†’ FALSE โŒ
    • FALSE && anything โ†’ FALSE โŒ (short-circuit)
  4. Overall: FALSE || FALSE โ†’ FALSE
  5. Go to else
  6. Output: "Not a Leap year"

Example 3: year = 2000 (400-year exception)

  1. Read: year = 2000
  2. Check: 2000 % 400 == 0
    • 2000 รท 400 = 5 remainder 0
    • TRUE โœ…
  3. Short-circuit! Second part not evaluated
  4. Overall: TRUE || anything โ†’ TRUE
  5. Output: "Leap year"

Example 4: year = 2013 (not divisible by 4)

  1. Read: year = 2013
  2. Check: 2013 % 400 == 0 โ†’ FALSE โŒ
  3. Check: (2013 % 100 != 0 && 2013 % 4 == 0)
    • 2013 % 100 = 13 (not 0) โ†’ TRUE โœ…
    • 2013 % 4 = 1 (not 0) โ†’ FALSE โŒ
    • TRUE && FALSE โ†’ FALSE โŒ
  4. Overall: FALSE || FALSE โ†’ FALSE
  5. Output: "Not a Leap year"
๐ŸŽจ Alternative Implementation Approaches โ–ผ

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.

๐ŸŒ Real-World Applications โ–ผ

Where leap year logic is used:

1. Calendar Applications:

  • Google Calendar, Outlook, Apple Calendar
  • Scheduling systems
  • Date pickers in websites

2. Financial Systems:

  • Interest calculations (366 days vs 365 days)
  • Payroll systems (bi-weekly payments)
  • Loan amortization schedules

3. Age Calculation:

  • Legal age verification
  • Birthday tracking
  • Insurance premium calculations

4. Event Planning:

  • Olympics (held every 4 years, often in leap years)
  • Presidential elections (US)
  • Long-term project timelines

5. Date Validation:

  • Checking if Feb 29 is valid
  • Database date fields
  • Form validation

6. Programming Languages:

  • Built-in date libraries use this logic
  • Python: calendar.isleap()
  • JavaScript: Date object calculations
  • Java: 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!

Solution

#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;
}