A year that has 366 days is called a leap year.
A year can be checked whether a year is leap year or not by dividing the year by 4, 100 and 400. If a number is divisible by 4 but not by 100 then, it is a leap year. Also, if a number is divisible by 4, 100 and 400 then it is a leap year. Otherwise the year is not a leap year.
Example 1: Source Code to Check Leap Year
#include <stdio.h> int yr; printf ("Enter a year n"); scanf ("%d", &yr); if (yr%4 == 0) { if(yr%100 == 0) { if(yr%400 == 0) printf("n It is LEAP YEAR."); else printf("n It is NOT LEAP YEAR."); } else { printf ("n It is LEAP YEAR."); } } else printf("n It is NOT LEAP YEAR."); return 0;
Here, the year entered by the user is firstly divided by 4. If it is divisible by 4 then it is divided by 100 and then 400. If year is divisible by all 3 numbers then that year is a leap year. If the year is divisible by 4 and 100 but not by 400 then it is not a leap year. If the year is divisible by 4 but not by 100, then it is a leap year. (Remember that if the year is divisible by 4 and not by hundred then the program does not check the last condition, i.e., whether the year is divisible by 400). If the year is not divisible by 4 then no other conditions are checked and the year is not a leap year.
Example 2: Source Code to Check Leap Year
#include <stdio.h> int main() { int yr; printf ("Enter a year n"); scanf ("%d", &yr); if (yr%4 == 0 && yr%100 == 0 && yr%400 == 0) printf("n It is LEAP YEAR."); else if (yr%4==0 && yr%100!=0) printf("n It is LEAP YEAR."); else printf ("n It is NOT LEAP YEAR."); return 0; }
Here, if the year is divisible by 4, 100 and 400 then “It is LEAP YEAR.” is displayed. If the year is divisible by 4 but not by 100 then “It is LEAP YEAR.” is displayed. Otherwise, “It is NOT LEAP YEAR” is displayed.
Output:
Enter a year 1600 It is LEAP YEAR.
Enter a year 2900 It is NOT LEAP YEAR.