Before knowing the C program for leap year lets first understand what exactly is a leap year.
What is a Leap Year?
In a normal year there are 365 days while in a leap year there are 366 days. Here the extra day is the 29th feb.
How to Check Year is a Leap Year or not?
- If the year is completely divisible by 4 then it is leap year. For example 2016, 2020, etc.
- It the year is century year (ending with 00) then it should be divisible by 400, only then it will be a leap year. For example 2000, 2004, etc.
C Program for Leap Year
Now lets have a look on its implementation in C language.
#include<stdio.h> void main() { int year; printf("Enter any year(4-digit):"); scanf("%d",&year); if(year%100==0) { if(year%400==0) printf("\nLeap Year"); else printf("\nNot Leap Year"); } else if(year%4==0) printf("\nLeap Year"); else printf("\nNot Leap Year"); }
Output
Enter any year(4-digit):2020
Leap Year
almost this code are right but not fully perfect
What is missing, let me know about it.
the logic used is incomplete
plz mention it in the comment.
the logic is
1) the year should be divisible by 400 and 100 ,that is; only 400 because the no divisible by 400 will also be divisible by 100
or
2) the no should be divisible by 4 and not divisible by 100.
so it should be
if(year%400==0||year%4==0&&year%100!=0);
thanks for your nice suggestion. 🙂