In the world of computer science, the concept of leap year plays a crucial role in various applications, especially when we are dealing with date and time calculation. A leap year occurs every four years to compensate for the fact that the Earth’s orbit around the sun takes approximately 365.25 days. In this article, we will write a C program that checks whether a given year is a leap year or not.
Table of Contents
The rule for Leap Year
The rule for identifying a leap year is relatively simple and straightforward. A year is considered a leap year, if it is divisible by 4. However, to avoid excessive leap years, years divisible by 100 are not a leap year unless they are also divisible by 400. This rule ensures that years like 1700, and 1800 are not considered leap years while the years 2000, and 2400 are considered leap years because they are divisible by both 100 and 400.
Leap Year Flow Chart
C Program to Check Leap Year
#include <stdio.h>
void main()
{
int year;
printf("Enter the year ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
printf("The given year %d is a leap year ",year);
}
else
{
printf("The given year %d is not a leap year ",year);
}
}
Output
How the Above C Program Works?
- The program asks the user to enter a year to check whether a given year is a leap year or not.
- With the help of the if statement and leap year condition
((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
program checks and prints whether the year entered by the user is a leap year or not.
C Program to Check Leap Year Using Ternary Operator
#include <stdio.h>
void main()
{
int year;
printf("Enter the year ");
scanf("%d", &year);
(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ?
printf("The given year %d is a leap year ", year)
:
printf("The given year %d is not a leap year ", year);
}
Output
In the above C Program the ternary operator (? :
) is used to check whether the entered year is a leap year or not.
Happy Coding!!
2 thoughts on “C Program to Check Leap Year”