Leap Year Check
Program to check if a year is a leap year
C++ Leap Year Check Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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?
---
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
Rule 2: Divisible by 400
Not a leap year:
Summary:
Leap year
✅
Not leap year
❌
Leap year
✅
Not leap year
❌
---
3. Header File: #include <iostream>
#include <iostream>
Provides:
cout → for displaying outputcin → for reading input---
4. Declaring Variables
int year;
---
5. Taking Input From User
cin >> year;
yearExample:
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)`
AND
not divisible by 100
Part 2: `(year % 400 == 0)`
Logical OR (`||`):
either
part is true, the entire condition is true
OR
Rule 2
---
7. Step-by-Step Examples
Example 1: 2024 (Regular Leap Year)
2024 % 4 == 0 → 0 == 0 →true
2024 % 100 != 0 → 24 != 0 →true
true && true →true
2024 % 400 == 0 → 24 == 0 →false
true || false →true
→
Leap year
✅
Example 2: 2000 (Century Leap Year)
2000 % 4 == 0 →true
2000 % 100 != 0 → 0 != 0 →false
true && false →false
2000 % 400 == 0 →true
false || true →true
→
Leap year
✅
Example 3: 1900 (Not a Leap Year)
1900 % 4 == 0 →true
1900 % 100 != 0 → 0 != 0 →false
true && false →false
1900 % 400 == 0 → 300 == 0 →false
false || false →false
→
Not leap year
❌
Example 4: 2023 (Not a Leap Year)
2023 % 4 == 0 → 3 == 0 →false
false && ... →false
(short-circuit)
2023 % 400 == 0 →false
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
Leap year
✅
Case 2: Divisible by 100 but not 400
year % 100 != 0 →false
(because divisible by 100)
false
year % 400 == 0 →false
(not divisible by 400)
false
Not leap year
❌
Case 3: Divisible by 4 but not 100
year % 4 == 0 →true
year % 100 != 0 →true
true
Leap year
✅
Case 4: Not divisible by 4
year % 4 == 0 →false
false
false
Not leap year
❌
---
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:
2024 is a leap year
2023 is not a leap year
2000 is a leap year
1900 is not a leap year
---
10. Common Mistakes to Avoid
Mistake 1: Only checking divisibility by 4
if (year % 4 == 0) // WRONG!
Mistake 2: Not handling century years
Mistake 3: Wrong operator precedence
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)---
11. Historical Context
Julian Calendar (old):
Gregorian Calendar (current):
---
12. Real-World Applications
Date calculations:
Calendar applications:
Time libraries:
---
Summary
This program teaches:
Understanding leap years is essential for:
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.