Beginner ยท boolean logic
Leap Year
The calendar's rule: divisible by 4, except century years, unless divisible by 400. One boolean expression decides it โ the century cases are the traps.
Problem
Input: one year y (1600 โค y โค 3000).
Output: Yes if y is a leap year, No otherwise.
Sample:
| Input | Output |
|---|---|
2024 | Yes |
1900 | No |
2000 | Yes |
Your solution
Custom input for "Run my input"
Judging runs entirely in your browser against hidden tests. Your code is saved locally as you type. Ctrl+Enter judges.
Stuck? Hints
Hint 1 โ the rule in words
Leap if divisible by 4 AND not by 100, OR divisible by 400. That's literally one boolean expression with && and ||.
Hint 2 โ the trap
1900 is divisible by 4 but is NOT leap; 2000 is divisible by 400 and IS. If your code only checks y % 4 == 0, both hidden tests will fail.
Solution (try first!)
int y = int.Parse(Console.ReadLine());
bool leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
Console.WriteLine(leap ? "Yes" : "No");
Boolean operators composing a real-world rule โ Lesson 3 material. Next: Reverse a Number.