Write a C program to check leap year using if else. How to check whether a given year is leap year or not in C programming. C Program to input year from user and check whether the given year is leap year or not using ladder if else. Logic to check leap year in C programming.
Input
Input year: 2004
Output
2004 is leap year.
Required knowledge
Basic C programming, Arithmetic operators, Relational operators, Logical operators, If else
Logic to check leap year
Wikipedia states leap year as a special year containing one extra day i.e. total 366 days in a year. A year is said to be leap year, if the year is exactly divisible by 4 but and not divisible by 100. Year is also a leap year if it is exactly divisible by 400.
Step by step descriptive logic to check leap year.
- Input year from user. Store it in some variable say year.
- If year is exactly divisible by 4 and not divisible by 100, then it is leap year. Or if year is exactly divisible by 400 then it is leap year.
Let us now implement the logic in our program.
Program to check leap year
/**
* C program to check Leap Year
*/
#include <stdio.h>
int main()
{
int year;
/* Input year from user */
printf("Enter year : ");
scanf("%d", &year);
/*
* If year is exactly divisible by 4 and year is not divisible by 100
* or year is exactly divisible by 400 then
* the year is leap year.
* Else year is normal year
*/
if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))
{
printf("LEAP YEAR");
}
else
{
printf("COMMON YEAR");
}
return 0;
}
Enhance your skills by learning this program using conditional operator.
Learn more – Program to check leap year using conditional operator.
Output
Enter year : 2004 LEAP YEAR
Happy coding 😉
Recommended posts
- If else programming exercises index.
- C program to check whether a number is negative, positive or zero.
- C program to check whether a character is alphabet or not.
- C program to check whether an alphabet is vowel or not.
- C program to check alphabet, digit and special character.
- C program to check uppercase or lowercase.