Check for Leap year in C#
How to determine whether a year is a leap year
To determine whether a year is a leap year, follow these steps:- If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
- If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
- If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
- The year is a leap year (it has 366 days).
- The year is not a leap year (it has 365 days).
C# Code
public bool isLeapYear(int year)
{
bool isLeap = true;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
Console.WriteLine("{0} is Leap Year", year);
}
else Console.WriteLine("{0} is Leap Year", year);
}
else
{
Console.WriteLine("{0} is not Leap Year", year);
isLeap = false;
}
return isLeap;
}
No comments:
Post a Comment