A year is a leap year if it is divisible by 4, except years divisible by 100, which are leap years only if they are also divisible by 400. Print YES or NO.
What to build
One condition that combines a rule with its exceptions.
Requirements
- Read a year.
- Print
YESfor a leap year andNOotherwise.
Input
One year from 1 to 9999.
Output
YES or NO.
Constraints
The year is from 1 to 9999.
Example
Input:
2024
Output:
YES
Why it works
A year is about 365.2425 days. The rule adds a day every 4 years, and the exceptions remove the small excess over the centuries.
Examples
Example 1 · Input
2024
Expected output
YES
Example 2 · Input
1900
Expected output
NO
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Combine conditions with and / or, and use brackets to make the order clear.
Solution
Show one correct solution (try it yourself first)
year = int(input())
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print("YES")
else:
print("NO")