Leap Year Check

Program to check if a year is a leap year

C++Beginner
C++
#include <iostream>
using namespace std;

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

Output

Enter a year: 2024
2024 is a leap year

Leap Year Check in C++

This program determines whether a given year is a leap year. A leap year has 366 days instead of the usual 365 days, with an extra day (February 29) added. Understanding leap year rules is important for date calculations, calendar applications, and time-related programming.

What is a Leap Year?

A leap year is a year that has 366 days instead of 365 days. The extra day is added to February (February 29).

Why do we need leap years?

  • Earth takes approximately 365.25 days to orbit the sun
  • Without leap years, our calendar would drift over time
  • After 100 years, we'd be off by about 25 days!
  • Leap years keep our calendar synchronized with Earth's orbit

Leap Year Rules

A year is a leap year if it satisfies ## ONE of these conditions:

Rule 1: Divisible by 4, but NOT by 100

  • Examples: 2020, 2024, 2028 (divisible by 4, not by 100) ✅

Rule 2: Divisible by 400

  • Examples: 2000, 2400 (divisible by 400) ✅

Not a leap year:

  • Divisible by 100 but NOT by 400
  • Examples: 1900, 2100, 2200 ❌

Summary:

  • If divisible by 400 → ## Leap year ✅
  • Else if divisible by 100 → ## Not leap year ❌
  • Else if divisible by 4 → ## Leap year ✅
  • Otherwise → ## Not leap year ❌

Understanding the Leap Year Condition

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

This condition combines two rules using logical operators.

Breaking it down:

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

  • Checks if year is divisible by 4 ## AND not divisible by 100
  • This handles regular leap years (2020, 2024, 2028, etc.)

Part 2: (year % 400 == 0)

  • Checks if year is divisible by 400
  • This handles century leap years (2000, 2400, etc.)

Logical OR (||):

  • If ## either part is true, the entire condition is true
  • Year is a leap year if it satisfies Rule 1 ## OR Rule 2

Summary

  • Leap year has 366 days (extra day: February 29)
  • Rules: Divisible by 4 (except centuries), OR divisible by 400
  • Century years (1900, 2000, 2100) need special handling
  • 2000 is leap year, but 1900 and 2100 are not
  • Use logical operators (&&, ||) to combine conditions
  • Always test with edge cases: 2000, 1900, 2024, 2023

This program teaches:

  • Complex conditional logic
  • Logical operators (&&, ||)
  • Modulo operator for divisibility checks
  • Handling special cases (century years)
  • Real-world date/time programming

Understanding leap years is essential for:

  • Date manipulation in programs
  • Calendar applications
  • Time-based calculations
  • Many real-world software applications