This is another interesting C program for beginner programmers, which is very useful for job hunters and school or college students.
Writing a C program to find leap year or not, Interesting isn’t it?
Formula for finding a Leap year is as follows:
1. Year should be evenly divisible by 4 (should not have a reminder):
year % 4 == 0 ( reminder should be 0 )
2. AND Year should not be evenly divisible by 100 (should have a reminder):
year % 100 != 0 ( reminder should not be 0 )
3. OR Year should be evenly divisible by 400 (should have a reminder):
year%400 == 0 ( reminder should be 0 )
So basically 1 && 2 || 3
if (1 and 2) both are true or 3 is true then it is a leap year
Here is the C program to find the leap year,
#include <stdio.h>
int main()
{
int year;
printf("C Program to check Leap year or not:\n");
printf("Enter a year:\n");
scanf("%d", &year);
if( (0 == year % 4) && (0 != year % 100) || (0 == year % 400) )
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
return 0;
}The output of the above program will be:
C Program to check Leap year or not:
Enter a year: 2012
2012 is a leap year
If you feel difficult to use && / ||, you can use and / or also, like this:
if( (0 == year % 4) and (0 != year % 100) or (0 == year % 400) )
{
printf("%d is a leap year.\n", year);
}