Leap year checker
Determine whether a year is a leap year under the Gregorian calendar rules.
Course progress
Problem 16 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
years = [2024, 1900]
Expected output
2024: True 1900: False
What is happening?
2024 is divisible by 4 and not by 100, so it is a leap year. Although 1900 is divisible by 100, it is not divisible by 400, so it is not.
Solution strategy
Build the right mental model
A leap year is divisible by 400, or it is divisible by 4 but not by 100. Test the century exception explicitly.
Core concepts
boolean logic · conditions
Complexity
Time O(1), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for leap-year
using System;
class Program
{
static bool IsLeapYear(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
static void Main()
{
Console.WriteLine("2024: " + IsLeapYear(2024));
Console.WriteLine("1900: " + IsLeapYear(1900));
}
}Knowledge check
Questions students often ask
What is the main idea behind leap year checker?
A leap year is divisible by 400, or it is divisible by 4 but not by 100. Test the century exception explicitly.
What is the time and space complexity of this solution?
Time O(1), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.