C program for a leap year
Written by
Leap Year Program:
A leap year has 366 days whereas a non-leap year has 365 days.
To check if a year is a leap year it should satisfy either of the two conditions:
- For years that are not century years (example – 2004, 2008, etc.), the year should be divisible by 4 and not divisible by 100 to be a leap year.
- For years that are century years (example – 1600, 2000, etc.), the year should be divisible by 400 to be a leap year.
The code for checking if a year is a leap year or not is:
#include <stdio.h>int main() {
int year;
printf("Enter year: ");
scanf("%d", & amp; year);
if (((year % 4 == 0) & amp; & amp; (year % 100 != 0)) || (year % 400 == 0))
printf("%d is a Leap year", year);
else
printf("%d is not a Leap Year", year);
return 0;
}
- We have created an integer variable year to store the user input for the year.
- We are simply checking if the year is divisible by 4 and not divisible by 100 OR if the year is divisible by 400. If any of these two conditions are satisfied, the year is a leap year; else, it is not a leap year.
- We use the modulus operator to check for divisibility. If a%b is equal to 0, it implies that a is perfectly divisible by b.
We run the code for different inputs and get the outputs accordingly.
Some inputs and outputs for the code are:
Enter year: 2002
2002 is not a Leap Year
Enter year: 2000
2000 is a Leap year
Enter year: 1993
1993 is not a Leap Year
Enter year: 1600
1600 is a Leap year