Thursday, 5 January 2017

C program to check leap year

A leap year comes once in every four years and once in four centuries. A very common mistake most of the programmers does is forgetting to check for centuries. Most of the time students will check whether the year is divisible by 4. If the given year is a century, then we must check whether it is divisible by 400.

//Program to check for leap year
#include<stdio.h>
#include<conio.h>
main()
{
 int year,lpyr=0;
 clrscr();
 printf("Enter a year\t");
 scanf("%d",&year);
 if (year%100==0)
   {
     lpyr=year%400;
     if (lpyr==0)
     {
       printf("The year %d is a leap year\n",year);
     }
     else
       printf("%d is not a leap year\n",year);
   }
 else
   {
     lpyr=year%4;
     if (lpyr==0)
     {
       printf("%d is a leap year\n",year);
     }
     else
       printf("%d is not a leap year\n",year);
   }
 getch();
}

/*
OUTPUT
--------
Enter a year    1900
1900 is not a leap year

Enter a year    2000
The year 2000 is a leap year

Enter a year    2003
2003 is not a leap year

Enter a year    2008
2008 is a leap year
*/

No comments:

Post a Comment