C Program to Check Leap Year
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
Example: Program to Check Leap Year
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
Output 1
Enter a year: 1900 1900 is not a leap year.
Output 2
Enter a year: 2012 2012 is a leap year.
program in C that checks whether a given year is a leap year or not:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
} else {
printf("%d is a leap year.\n", year);
}
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
In this program, we declare an integer variable “year”, which represents the year we want to check for leap year. We prompt the user to enter this value using the “scanf” function.
We then use a series of nested “if” statements to determine whether the year is a leap year. According to the Gregorian calendar, a year is a leap year if it is divisible by 4, except for years that are divisible by 100 but not divisible by 400. This means that, for example, the year 2000 was a leap year, but the year 1900 was not.
We first check whether the year is divisible by 4. If it is, we check whether it is divisible by 100. If it is, we check whether it is divisible by 400. If it is, we print out a message indicating that the year is a leap year. If it is not divisible by 400, we print out a message indicating that it is not a leap year. If the year is divisible by 4 but not by 100, we print out a message indicating that it is a leap year. Finally, if the year is not divisible by 4, we print out a message indicating that it is not a leap year.
Finally, we return 0 from the main function to indicate that the program has been executed successfully. The output of this program would be:
Enter a year: 2000
2000 is a leap year.
If the user had entered a non-leap year, such as 1900, the output would be:
Enter a year: 1900
1900 is not a leap year.