0
0
CsharpProgramBeginner · 2 min read

C# Program to Check Leap Year with Output and Explanation

You can check a leap year in C# by using the condition if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) to print if the year is leap or not.
📋

Examples

Input2000
Output2000 is a leap year.
Input1900
Output1900 is not a leap year.
Input2024
Output2024 is a leap year.
🧠

How to Think About It

To check if a year is leap, first see if it is divisible by 400. If yes, it is leap. If not, check if it is divisible by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
📐

Algorithm

1
Get the year as input.
2
Check if the year is divisible by 400. If yes, it is a leap year.
3
Else, check if the year is divisible by 4 and not divisible by 100. If yes, it is a leap year.
4
Otherwise, it is not a leap year.
5
Print the result.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a year: ");
        int year = int.Parse(Console.ReadLine());
        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
            Console.WriteLine($"{year} is a leap year.");
        } else {
            Console.WriteLine($"{year} is not a leap year.");
        }
    }
}
Output
Enter a year: 2024 2024 is a leap year.
🔍

Dry Run

Let's trace the year 1900 through the code

1

Input year

year = 1900

2

Check divisibility by 400

1900 % 400 = 300 (not zero), so false

3

Check divisibility by 4 and not by 100

1900 % 4 = 0 (true) and 1900 % 100 = 0 (true), so condition false because year % 100 == 0

4

Determine result

Not a leap year

CheckResult
1900 % 400 == 0False
1900 % 4 == 0True
1900 % 100 != 0False
Final leap year?No
💡

Why This Works

Step 1: Divisible by 400 means leap year

If a year divides evenly by 400, it is always a leap year, so we check year % 400 == 0 first.

Step 2: Divisible by 4 but not by 100 means leap year

If the year is divisible by 4 but not by 100, it is a leap year, so we check year % 4 == 0 && year % 100 != 0.

Step 3: Otherwise not a leap year

If neither condition is true, the year is not a leap year.

🔄

Alternative Approaches

Using DateTime.IsLeapYear method
csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a year: ");
        int year = int.Parse(Console.ReadLine());
        if (DateTime.IsLeapYear(year)) {
            Console.WriteLine($"{year} is a leap year.");
        } else {
            Console.WriteLine($"{year} is not a leap year.");
        }
    }
}
This uses built-in .NET method for simplicity and reliability but requires .NET Framework or .NET Core.
Using nested if statements
csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a year: ");
        int year = int.Parse(Console.ReadLine());
        if (year % 400 == 0) {
            Console.WriteLine($"{year} is a leap year.");
        } else {
            if (year % 100 == 0) {
                Console.WriteLine($"{year} is not a leap year.");
            } else if (year % 4 == 0) {
                Console.WriteLine($"{year} is a leap year.");
            } else {
                Console.WriteLine($"{year} is not a leap year.");
            }
        }
    }
}
This approach is more verbose but clearly separates each condition.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic and logical operations regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store the year and results, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time, but using DateTime.IsLeapYear is simplest and less error-prone.

ApproachTimeSpaceBest For
Manual condition checkO(1)O(1)Learning logic and control flow
DateTime.IsLeapYear methodO(1)O(1)Clean, reliable production code
Nested if statementsO(1)O(1)Clear step-by-step logic
💡
Use the built-in DateTime.IsLeapYear(year) method for cleaner code.
⚠️
Beginners often forget to check the year % 100 != 0 condition, causing incorrect leap year results.