C++ Program to Check Leap Year with Explanation
C++
Medium
Control Flow
37 views
1 min read
168 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around year, leap, check. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Check if given year is leap year or not.
Real Life: Like February having 29 days in some years.
Input Format
An integer year.
Output Format
Print
• Leap Year
OR
• Not a Leap Year
Constraints
• year > 0
Concept Explanation
A leap year decides whether February has 28 or 29 days.
Leap year rules are fixed and must be checked in order.
Step-by-Step Logic
1.Take input year.
2.If year is divisible by 400:
• It is a Leap Year.
3.Else if year is divisible by 100:
• It is Not a Leap Year.
4.Else if year is divisible by 4:
• It is a Leap Year.
5.Else:
• It is Not a Leap Year.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
void control_q6_leap_year() {
int year = 2024;
bool isLeap = false;
if(year % 400 == 0) {
isLeap = true;
} else if(year % 100 == 0) {
isLeap = false;
} else if(year % 4 == 0) {
isLeap = true;
}
if(isLeap) {
cout << year << " is a Leap Year" << endl;
} else {
cout << year << " is NOT a Leap Year" << endl;
}
}
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Check if given year is leap year or not.
Real Life: Like February having 29 days in some years.
Input / Output
Output
Print
• Leap Year
OR
• Not a Leap Year
Explanation
Concept Explanation
A leap year decides whether February has 28 or 29 days.
Leap year rules are fixed and must be checked in order.
Step-by-Step Explanation
1.Take input year.
2.If year is divisible by 400:
• It is a Leap Year.
3.Else if year is divisible by 100:
• It is Not a Leap Year.
4.Else if year is divisible by 4:
• It is a Leap Year.
5.Else:
• It is Not a Leap Year.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
void control_q6_leap_year() {
int year = 2024;
bool isLeap = false;
if(year % 400 == 0) {
isLeap = true;
} else if(year % 100 == 0) {
isLeap = false;
} else if(year % 4 == 0) {
isLeap = true;
}
if(isLeap) {
cout << year << " is a Leap Year" << endl;
} else {
cout << year << " is NOT a Leap Year" << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!