- Write a c program to check whether a year is leap year or not.
A leap year is a year containing one additional day in order to keep the calendar year in sync with the astronomical year. Each leap year lasts 366 days instead of the usual 365, by extending February to 29 days rather than the common 28 days.
Example of leap years: 1980, 1984, 1988, 1992, 1996, 2000
Algorithm to check whether a year is leap year or not
- If a year is divisible by 4 but not by 100, then it is a leap year.
- If a year is divisible by both 4 and by 100, then it is a leap year only if it is also divisible by 400.
C program to check whether a year is leap year or not
This program takes a year as input from user and checks whether it is leap year or not as per above mentioned algorithm and print it accordingly.
/* * C Program to check a year is leap year or not */ #include <stdio.h> #include <conio.h> int main(){ int year; printf("Enter a year for leap year check\n"); scanf("%d", &year); /* * if (year is not divisible by 4) then (it is a common year) * else if (year is not divisible by 100) then (it is a leap year) * else if (year is not divisible by 400) then (it is a common year) * else (it is a leap year) */ if(year%4 != 0){ printf("%d is not a leap year\n", year); } else { 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 ); } } getch(); return 0; }
Program Output
Enter a year for leap year check 1983 1983 is not a leap year Enter a year for leap year check 2016 2016 is a leap year
C program to check whether a year is leap year or not in one line
/* * C Program to check a year is leap year or not in one line */ #include <stdio.h> #include <conio.h> int main(){ int year; printf("Enter a year for leap year check\n"); scanf("%d", &year); if(((year%4==0)&&(year%100!=0))||(year%400==0)){ /* Entered year is a leap year */ printf("%d is leap year\n", year); } else { /* Entered year is not a leap year */ printf("%d is not leap year\n", year); } getch(); return 0; }