A leap year, occurring every four years, has 366 days instead of the usual 365, thanks to an extra day in February. Do you want to check a leap year in C#? In this tutorial, I will explain two methods to find a leap year in C#. So, let us start with writing a program to find a leap year in C#.
To determine if a year is a leap year in C#, you can use a simple method that checks three conditions: if the year is divisible by 4, not divisible by 100, except when it’s also divisible by 400. The code snippet bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); efficiently performs this check, returning true for a leap year and false otherwise. This concise approach ensures accurate leap year identification in C#.
Write a Program to Find a Leap Year in C#
First, let us understand the rules that define a leap year:
- A year is a leap year if it is divisible by 4.
- However, if the year is also divisible by 100, it is not a leap year.
- But, if it’s divisible by 400, it is a leap year.
Here is a complete program to check a leap year in C#.
using System;
class LeapYearProgram
{
static void Main(string[] args)
{
Console.WriteLine("Enter a year to check if it's a leap year:");
int year = Convert.ToInt32(Console.ReadLine());
if (IsLeapYear(year))
{
Console.WriteLine($"{year} is a leap year.");
}
else
{
Console.WriteLine($"{year} is not a leap year.");
}
}
static bool IsLeapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return true;
}
return false;
}
}
- Main Method: This is where the program starts. It prompts the user to enter a year and then calls the IsLeapYear method.
- IsLeapYear Method: This method encapsulates the leap year logic. It returns true if the year is a leap year and false otherwise.
Once you run the code, it will ask you to enter a year to check if it is a leap year or not. Then, it will check and show the output, like in the screenshot below. I inserted 2024, and it is showing me it is a leap year.
Find a Leap Year in C# using DateTime.IsLeapYear Method
C# provides a built-in method in the System namespace to check if a year is a leap year. This method abstracts away the underlying calculations and provides a direct way to determine leap years. So, here is another program to check if a year is a leap year in C# using the DateTime.IsLeapYear() method.
In this approach, you simply pass the year to the DateTime.IsLeapYear method, and it returns true if the year is a leap year or false otherwise.
using System;
class LeapYearChecker
{
static void Main()
{
Console.WriteLine("Enter a year to check if it's a leap year:");
int year = Convert.ToInt32(Console.ReadLine());
if (DateTime.IsLeapYear(year))
{
Console.WriteLine($"{year} is a leap year.");
}
else
{
Console.WriteLine($"{year} is not a leap year.");
}
}
}
Here, you can see the output in the screenshot below. I entered 2024 as a year, and it is showing 2024 as a leap year.
In the next screenshot below, you can see I entered 2025, and it shows me 2025 is not a leap year after I executed the above C# program.
Conclusion
The built-in DateTime.IsLeapYear() method is the most straightforward and reliable approach provided by the .NET framework to find a leap year in C#. I hope you got to know how to write a program to find a leap year in C#. We have explained here 2 different methods to check a leap year in C#. A lot of time, you will find this is C# Interview Questions for 5 Years Experience.
You may also like:
- Write A Program To Find Numbers Above And Below The Average In C#
- Write A Program To Find Second Largest Number In An Array In C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…