Check Leap year program in C Language
Introduction:
In this article, We will look at the Leap Year program in C Language. We also look at the algorithm for the Leap Year.
What is Leap Year :
- A leap year is a year containing one additional day in the month of February (Leap year has 366 days and February month consists 29 days instead of 28).
Formula to check Leap Year :
Generally we all know that, If a year is perfectly divisible by 4 then that year is called as Leap year. But is it correct ?? No. because the years which are divisible by 100 are not leap years , Except the years like 400,800,..( i.e years which are divisible by 400).
If your confused here is the small Algorithm to calculate Leap year .
Algorithm to Check Leap Year Program:
- If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
- If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
- If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
- The year is a leap year (it has 366 days).
- The year is not a leap year (it has 365 days).
This algorithm is saying, if a year is divisible by 4 it is Leap year but we have few conditions those are ( Note : Below two conditions apply only for years like 100,200,300,400,500,…etc)
- if a year is divisible by 100 and 400 then it is Leap Year. ex : 400,800
- if a year is divisible by 100 and not divisible by 400 then it is not Leap year.
Another algorithm to Check Leap Year Program:
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)
Hope Now you know what is Leap year and How to calculate the Leap year.
Now we will try to write a C program to that takes one year as Input and it will decides weather it is Leap year or not.
Program : C program to Check given year is Leap year or not :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> int main() { int year; // Take the Input from the user, Store it in 'year' variable printf("Enter a year to check if it is a leap year : "); scanf("%d", &year); // Check if the year is divisable by '400', // If it is perfactly divisable by '400', Then it is a Leap Year if ( year%400 == 0) { printf("%d is a leap year.\n", year); } else if ( year%100 == 0) { // If 'year' is not divisable by 400, but divisable by 100 // Then it is not a Leap Year printf("%d is not a leap year.\n", year); } else if ( year%4 == 0 ) { // If 'year' is perfactly divisable by '4', And not a Century year // Then it is a Leap Year printf("%d is a leap year.\n", year); } else { // If any of the above Conditions are not True // Then given Year is 'Not a Leap' Year printf("%d is not a leap year.\n", year); } return 0; } |
Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
venkey@venkey$ ./a.out Enter a year to check if it is a leap year : 2000 2000 is a leap year. venkey@venkey$ ./a.out Enter a year to check if it is a leap year : 1900 1900 is not a leap year. venkey@venkey$ ./a.out Enter a year to check if it is a leap year : 2001 2001 is not a leap year. venkey@venkey$ ./a.out Enter a year to check if it is a leap year : 2021 2021 is not a leap year. venkey@venkey$ ./a.out Enter a year to check if it is a leap year : 2024 2024 is a leap year. venkey@venkey$ ./a.out Enter a year to check if it is a leap year : 2016 2016 is a leap year. venkey@venkey$ |