Check Leap Year in C#

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Modulo rules

What you’ll learn

  • The Gregorian leap-year rules: divisible by 4, century exception, divisible by 400.
  • How to implement IsLeapYear in C# with one clear boolean expression.
  • How to list leap years in a range (20242050) 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 bool methods, Console.WriteLine.
  • For Example 2: a for loop 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.

2024 ÷ 4, leap
1900 ÷ 100, not leap
2000 ÷ 400, leap

Formal rule

Year y is leap when: (y mod 4 = 0 AND y mod 100 ≠ 0) OR (y mod 400 = 0).

C# one-liner

(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

Quick examples

2024 Leap
Rule
Divisible by 4, not century
2023 Not leap
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.

Try 2024 (leap), 1900 (not), or 2000 (leap).

Live result
Press “Check year” to evaluate.

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

Pseudocode
function isLeapYear(year):
    return (year mod 4 = 0 AND year mod 100 ≠ 0) OR (year mod 400 = 0)
1

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.

c#
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 != 0

Regular leap rule. Covers 2024, 2028, etc., but excludes 1900.

2

Leap years from 2024 to 2050

Loops through the range and prints every leap year — same output as the reference tutorial.

c#
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

A year with 366 days instead of 365 — an extra day (February 29) keeps our calendar aligned with Earth's orbit.
A year is leap if it is divisible by 4, except century years (divisible by 100) which must also be divisible by 400.
Yes. 2024 ÷ 4 = 506 with no remainder, and 2024 is not a century year.
No. 1900 is divisible by 100 but not by 400.
Yes. 2000 is divisible by 400.
No — divisible by 100 but not 400, same pattern as 1900.
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); — the standard Gregorian rule in one expression.

🔄 Input / output examples

Change year in Main, or read from console with int.TryParse.

Year÷ 4÷ 100÷ 400Leap?
2024YesNoNoYes
2023NoNoNoNo
1900YesYesNoNo
2000YesYesYesYes
2100YesYesNoNo

Edge cases and pitfalls

The century exception is the part most beginners forget — test 1900 and 2000 explicitly.

Century

1900 vs 2000

Both divide by 4 and 100; only 2000 also divides by 400 — the classic trap.

Order

Wrong formula

year % 4 == 0 alone marks 1900 as leap — you must exclude or override century years.

Negative

year < 0

Modulo still works in C# for negatives, but calendar years are positive — validate input in real apps.

Built-in

DateTime.IsLeapYear

Useful in production; interviews still expect you to derive the modulo rule.

⏱️ Time and space complexity

OperationTimeExtra space
Single IsLeapYearO(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: 1900 not leap, 2000 leap — the 400 exception.
  • Range: loop and reuse IsLeapYear for listings like 2024–2050.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful