Leap Year Calculator

Level: Beginner 15–30 min

Concepts: Validation


Create a program to determine if a given year is a leap year.

Requirements

  1. 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
  2. Handle input validation:
    • Input must be a positive integer
    • Input must be a valid year (e.g., not negative)
  3. Return appropriate results:
    • Boolean indicating if the year is a leap year
    • Error message for invalid inputs

Test Cases

YearIs Leap Year?Reason
2000YesDivisible by 400
2004YesDivisible by 4, not by 100
2100NoDivisible by 100, not by 400
2001NoNot divisible by 4
0ErrorInvalid year
-1ErrorInvalid 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