Challenge: Leap Year
Completed
Read a year and print leap if it is a leap year, otherwise not leap.
A year is a leap year if it can be divided by 4, except years divisible by 100, unless they are also divisible by 400. (So 2024 and 2000 are leap years, 1900 is not.)
Input
One integer: the year.
Output
leap or not leap.
Example
Input
2024
Output
leap
Editor
C++
Hints
"divisible by 4" means year % 4 == 0.
Leap if (divisible by 4 AND not by 100) OR divisible by 400.
Solution explanation
Combine the rules with && and ||. Brackets make the order clear.
Show solution
#include <iostream>
using namespace std;
int main() {
int year;
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
cout << "leap\n";
} else {
cout << "not leap\n";
}
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module