Leap Year Calculator
Level: BeginnerConcepts: Validation
Create a program to determine if a given year is a leap year.
Requirements
- Implement leap year rules:
- A year is a leap year if it is divisible by 4
- However, if the year is divisible by 100, it is not a leap year
- Unless the year is also divisible by 400, then it is a leap year
- Handle input validation:
- Input must be a positive integer
- Input must be a valid year (e.g., not negative)
- Return appropriate results:
- Boolean indicating if the year is a leap year
- Error message for invalid inputs
Test Cases
Year | Is Leap Year? | Reason |
---|---|---|
2000 | Yes | Divisible by 400 |
2004 | Yes | Divisible by 4, not by 100 |
2100 | No | Divisible by 100, not by 400 |
2001 | No | Not divisible by 4 |
0 | Error | Invalid year |
-1 | Error | Invalid year |
Edge Cases to Consider
- Year 0
- Negative years
- Non-integer inputs
- Very large years
- Null or undefined inputs
Tips
- Start with the basic divisible by 4 rule
- Add the divisible by 100 exception
- Finally add the divisible by 400 rule
- Consider using a separate method for input validation
- Use descriptive variable names for the rules