Introduction
In this article, we will learn how to check whether a given year is a leap year in C#. Leap years are important in calendars because they keep our dates aligned with the Earth's revolutions around the sun. This is a beginner-friendly guide with clear explanations, C# examples, and practical tips.
What is a Leap Year?
A leap year is a year that has 366 days instead of 365. The extra day is added to February, making it 29 days long. The rules for leap years are:
-
If a year is divisible by 4, it may be a leap year.
-
If it is divisible by 100, it is not a leap year.
-
But if it is divisible by 400, then it is a leap year.
Examples
-
2000 → Leap year ? (divisible by 400)
-
1900 → Not a leap year ? (divisible by 100 but not by 400)
-
2024 → Leap year ? (divisible by 4 but not by 100)
-
2023 → Not a leap year ?
C# Program to Check Leap Year
Here’s a simple console application that checks whether the given year is a leap year or not:
using System;
namespace CheckLeapYear
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Year : ");
int Year = int.Parse(Console.ReadLine());
if (((Year % 4 == 0) && (Year % 100 != 0)) || (Year % 400 == 0))
Console.WriteLine("{0} is a Leap Year.", Year);
else
Console.WriteLine("{0} is not a Leap Year.", Year);
Console.ReadLine();
}
}
}
Output
Enter Year : 2024
2024 is a Leap Year.
Alternative Approach using DateTime.IsLeapYear()
Instead of writing custom logic, you can directly use the built-in DateTime.IsLeapYear(int year) method in C#. It returns true if the given year is a leap year, otherwise false.
using System;
class Program
{
static void Main()
{
int year = 2024;
if (DateTime.IsLeapYear(year))
Console.WriteLine($"{year} is a Leap Year.");
else
Console.WriteLine($"{year} is not a Leap Year.");
}
}
This method also validates input and throws an exception if the year is not between 1 and 9999.
Summary
Checking leap years in C# can be done using simple conditional logic or the built-in DateTime.IsLeapYear() method. Both approaches are efficient, and beginners are encouraged to start with the basic if-else approach to strengthen their understanding of conditions and operators. Once comfortable, using the built-in method makes code cleaner and easier to maintain. Understanding leap years is not only a common interview question but also a practical exercise for mastering C# programming basics.
Practice Challenge
Test what you’ve learned by solving beginner-friendly C# coding problems here: C# Beginner Challenges
Frequently Asked Questions
Q 1: How do you check leap year in C#?
Use an if-else condition with % modulus operator, or simply use DateTime.IsLeapYear(year).
Q 2: What is the logic for leap year in C#?
A year is a leap year if it is divisible by 4 and not 100, OR divisible by 400.
Q 3: Which built-in method checks a leap year in C#?
DateTime.IsLeapYear(int year) returns true if the given year is a leap year.
Q 4: Can I check a leap year without writing custom logic?
Yes, using DateTime.IsLeapYear(year) makes it easier and cleaner.
Q 5: What are some leap year examples?
Leap years: 2000, 2004, 2008, 2012, 2016, 2020, 2024.
Non-leap years: 1900, 2100, 2023.
