- Rule
- Divisible by 4, not century
Check Leap Year in C#
What you’ll learn
- The Gregorian leap-year rules: divisible by
4, century exception, divisible by400. - How to implement
IsLeapYearin C# with one clear boolean expression. - How to list leap years in a range (
2024–2050) and test any year with a live preview.
Overview
Leap-year checks are a classic conditional-logic exercise. You combine three divisibility tests with && and || — no loops required for a single year, though a loop helps when scanning a range.
Two programs
Check 2024 and list leap years from 2024 to 2050.
Live preview
Type a year and see which rule (4, 100, 400) decides the outcome.
Interview staple
Tests modulo, boolean logic, and whether you remember the century exception.
Prerequisites
Modulo (%), boolean && / ||, and basic if statements.
using System;,static boolmethods,Console.WriteLine.- For Example 2: a
forloop over a year range.
What is a leap year?
A leap year has 366 days — February gets an extra day (the 29th) so the calendar stays aligned with Earth’s orbit around the Sun.
Under the Gregorian calendar: leap if divisible by 4, unless it is a century year (divisible by 100) that is not also divisible by 400.
Formal rule
Year y is leap when: (y mod 4 = 0 AND y mod 100 ≠ 0) OR (y mod 400 = 0).
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
Quick examples
- Rule
- 2023 % 4 = 3
Leap years 2024–2050: 2024, 2028, 2032, 2036, 2040, 2044, 2048 (every 4 years until the century rule matters again at 2100).
Live preview
Enter a year (e.g. 2024, 1900, 2000). The widget evaluates each divisibility test.
Algorithm
Goal: return whether integer year is a Gregorian leap year.
Divisible by 400?
If year % 400 == 0, return leap (handles century leap years like 2000).
Century check
If year % 100 == 0, return not leap (e.g. 1900, 2100).
Divisible by 4?
Otherwise return whether year % 4 == 0. (Equivalent to the compact formula in one line.)
📜 Pseudocode
function isLeapYear(year):
return (year mod 4 = 0 AND year mod 100 ≠ 0) OR (year mod 400 = 0) Single year: is 2024 a leap year?
Reference solution with the standard boolean expression. Uses class Program for consistency with other tutorials on this site.
using System;
class Program
{
static bool IsLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
static void Main()
{
int year = 2024;
if (IsLeapYear(year))
{
Console.WriteLine($"{year} is a leap year.");
}
else
{
Console.WriteLine($"{year} is not a leap year.");
}
}
} Explanation
The expression short-circuits: century years fail the % 100 != 0 part unless the % 400 == 0 clause saves them (as with 2000).
year % 4 == 0 && year % 100 != 0Regular leap rule. Covers 2024, 2028, etc., but excludes 1900.
Leap years from 2024 to 2050
Loops through the range and prints every leap year — same output as the reference tutorial.
using System;
class Program
{
static bool IsLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
static void Main()
{
int startYear = 2024;
int endYear = 2050;
Console.WriteLine($"Leap years in the range {startYear} to {endYear}:");
for (int year = startYear; year <= endYear; year++)
{
if (IsLeapYear(year))
{
Console.Write($"{year} ");
}
}
Console.WriteLine();
}
} Explanation
2050 is not leap (2050 % 4 = 2). The loop reuses IsLeapYear so the rule lives in one place.
Beyond the basics
Step-by-step if chain. Check % 400 first, then % 100, then % 4 — easier to explain aloud in interviews.
DateTime.IsLeapYear. .NET has a built-in: DateTime.IsLeapYear(year) — mention it, but write the modulo version by hand in coding rounds.
Validation: reject years before 1582 if you need strict Gregorian history; for interview practice, any positive int is fine.
❓ FAQ
🔄 Input / output examples
Change year in Main, or read from console with int.TryParse.
| Year | ÷ 4 | ÷ 100 | ÷ 400 | Leap? |
|---|---|---|---|---|
2024 | Yes | No | No | Yes |
2023 | No | No | No | No |
1900 | Yes | Yes | No | No |
2000 | Yes | Yes | Yes | Yes |
2100 | Yes | Yes | No | No |
Edge cases and pitfalls
The century exception is the part most beginners forget — test 1900 and 2000 explicitly.
1900 vs 2000
Both divide by 4 and 100; only 2000 also divides by 400 — the classic trap.
Wrong formula
year % 4 == 0 alone marks 1900 as leap — you must exclude or override century years.
year < 0
Modulo still works in C# for negatives, but calendar years are positive — validate input in real apps.
DateTime.IsLeapYear
Useful in production; interviews still expect you to derive the modulo rule.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
Single IsLeapYear | O(1) | O(1) |
| Range scan (n years) | O(n) | O(1) |
Each check is a fixed number of modulo operations — constant time per year.
Summary
- Rule: leap if
(y % 4 == 0 && y % 100 != 0) || y % 400 == 0. - Remember:
1900not leap,2000leap — the400exception. - Range: loop and reuse
IsLeapYearfor listings like 2024–2050.
We add February 29 because Earth’s year is about 365.25 days, not exactly 365. The 400 rule fixes drift from century years like 1900 (not leap) while keeping 2000 (leap) on track with the Gregorian calendar.
5 people found this page helpful
