Leap Year Check

Program to check if a year is a leap year

BeginnerTopic: Conditional Programs
Back

C++ Leap Year Check Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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

Understanding Leap Year Check

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.

---

1. 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

---

2. 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

---

3. Header File: #include <iostream>

#include <iostream>

Provides:

cout → for displaying output
cin → for reading input

---

4. Declaring Variables

int year;

Stores the year entered by the user
Integer type is appropriate since years are whole numbers

---

5. Taking Input From User

`cout << "Enter a year: ";`

cin >> year;

Prompts user to enter a year
Reads and stores it in year

Example:

User enters:

2024

year = 2024

---

6. 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

---

7. Step-by-Step Examples

Example 1: 2024 (Regular Leap Year)

Check: 2024 % 4 == 00 == 0

true

Check: 2024 % 100 != 024 != 0

true

Part 1: true && true

true

Part 2: 2024 % 400 == 024 == 0

false

Result: true || false

true

Leap year

Example 2: 2000 (Century Leap Year)

Check: 2000 % 4 == 0

true

Check: 2000 % 100 != 00 != 0

false

Part 1: true && false

false

Part 2: 2000 % 400 == 0

true

Result: false || true

true

Leap year

Example 3: 1900 (Not a Leap Year)

Check: 1900 % 4 == 0

true

Check: 1900 % 100 != 00 != 0

false

Part 1: true && false

false

Part 2: 1900 % 400 == 0300 == 0

false

Result: false || false

false

Not leap year

Example 4: 2023 (Not a Leap Year)

Check: 2023 % 4 == 03 == 0

false

Part 1: false && ...

false

(short-circuit)

Part 2: 2023 % 400 == 0

false

Result: false || false

false

Not leap year

---

8. Why This Logic is Correct

The condition handles all cases:

Case 1: Divisible by 400

year % 400 == 0

true

Result:

Leap year

Examples: 2000, 2400

Case 2: Divisible by 100 but not 400

year % 100 != 0

false

(because divisible by 100)

Part 1:

false

year % 400 == 0

false

(not divisible by 400)

Part 2:

false

Result:

Not leap year

Examples: 1900, 2100

Case 3: Divisible by 4 but not 100

year % 4 == 0

true

year % 100 != 0

true

Part 1:

true

Result:

Leap year

Examples: 2020, 2024

Case 4: Not divisible by 4

year % 4 == 0

false

Part 1:

false

Part 2:

false

Result:

Not leap year

Examples: 2021, 2023

---

9. Displaying the Result

If condition is true:

cout << year << " is a leap year" << endl;

If condition is false (else):

cout << year << " is not a leap year" << endl;

Examples:

Input: 2024 → Output:

2024 is a leap year

Input: 2023 → Output:

2023 is not a leap year

Input: 2000 → Output:

2000 is a leap year

Input: 1900 → Output:

1900 is not a leap year

---

10. Common Mistakes to Avoid

Mistake 1: Only checking divisibility by 4

if (year % 4 == 0) // WRONG!

This would incorrectly mark 1900, 2100 as leap years ❌

Mistake 2: Not handling century years

Must check for 400 divisibility for century years
2000 is leap year, but 1900 is not

Mistake 3: Wrong operator precedence

Use parentheses to ensure correct evaluation
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

---

11. Historical Context

Julian Calendar (old):

Simple rule: Every 4 years is a leap year
Problem: Too many leap years (drift over time)

Gregorian Calendar (current):

Our current rule (divisible by 4, except centuries, except divisible by 400)
More accurate, keeps calendar synchronized

---

12. Real-World Applications

Date calculations:

Finding number of days between dates
Calculating age accurately
Scheduling applications

Calendar applications:

Displaying correct number of days in February
Showing leap year indicators
Date picker components

Time libraries:

All programming languages have leap year logic
Used in date/time manipulation functions

---

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

This is a practical program that demonstrates how mathematical rules translate into programming logic, and it's used in almost every application that deals with dates and times.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Leap Year Check

This C++ program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents