Thursday, 25 September 2014

C Program to check Leap Year

A leap year is a year containing one additional day in order to keep the calendar year synchronized with the astronomical or seasonal year.

For example, in the Gregorian calendar, each leap year lasts 366 days instead of the usual 365, by extending February to 29 days rather than the common 28 days.

For example:
1991 is not  a leap year.
2000  is a leap year.
1900  is not a leap year.
2004 is  a leap year.
Source Code

leap.c


#include <stdio.h>

#include <stdio.h>

int main() {
    int year;

    clrscr();
    printf("Enter a year to check:");
    scanf("%d", &year);

    if ( year%400 == 0)
        printf("\n%d is a leap year.\n", year);
    else if ( year%100 == 0)
        printf("\n%d is not a leap year.\n", year);
    else if ( year%4 == 0 )
        printf("\n%d is a leap year.\n", year);
    else
        printf("\n%d is not a leap year.\n", year);

    printf("\n\nPress any key to continue...", year);

    getch();

  return 0;
}



Output


Enter a year to check:1991
1991 is not a leap year.
Press any key to continue...

Enter a year to check:2000
2000 is a leap year.
Press any key to continue...

Enter a year to check:1900
1900 is not a leap year.
Press any key to continue...

Enter a year to check:2004
2004 is a leap year.
Press any key to continue...

Here :

1991 is not divisible by 4 and so it is not a leap year.
2000 is divisible by 400 and so it is a leap year even if it is a century (all centuries divisible by 400 are are  leap years).
1900 is a century and it is not divisible by 400 and  so it is not a leap year even it is divisible by 4. (all centuries are not leap years unless they are divisible by 400)
2004 is divisible by 4 and so it is  a leap year.

No comments:

Post a Comment