Loading ...

Find Leap year in ASP.NET(C#) | CodeAsp.Net

Find Leap year in ASP.NET(C#)

 /5
0 (0votes)

Leap year a year which have the 1 extra day from normal year, in leap year February have 29 days, in normal February have 28 day. Leap year has 366 days but normal year have 365.
To find the given year is the leap year, for that the algorithm is:
1. Year should be completely divided by 4 i.e. reminder should be zero.
2. AND, Year should not be completely divided by 100 i.e. reminder should not be zero.
3. OR, Year (Centaury) should be completely divided by 400 i.e. reminder should be zero.
We can find out the leap year in C# as follows for the given year.

private static string FindLeapYear(int year)

 {

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

    {

      return "Leap Year";

    }

     return "Not Leap Year";

 }

Example 1:
Response.Write(FindLeapYear(1600));
OUTPUT:
Leap Year

Example 2:
Response.Write(FindLeapYear(1000));
OUTPUT:
Not Leap Year

Example 3:
Response.Write(FindLeapYear(2008));
OUTPUT:
Leap Year

Comments (4)

   
hajan
Nice method, but using DateTime.IsLeapYear(year); will do the same .
6/15/2010
 · 
by
   
Chandrakant
Chandrakant
Is 200 is not a leap year?
7/24/2010
 · 
by
   
Vijjendra
@Chandrakant: no,for a leap year year must follow the given(in blog) 3 points.Year should be completely divided by 4 i.e. reminder should be zero.200 follow above condition, but did not follow below condition.Year should not be completely divided by 100 i.e. reminder should not be zero. That's why 200 is not a leap year.
7/24/2010
 · 
by
   
You created the Project Triangle concept really super explanation and the last paragraph is 100 percent exaclty fit. Regards,Kumares
8/30/2010
 · 
by
  • :*
  • :*
  • :
 *

Top Posts