Write a program to check leap year in C
In this post, you will learn different ways to check whether a particular year is a leap year or not using the C programming language.
A leap year is 366 days old rather than 365 days, in light of the fact that February is about 29 days long instead of the normal 28. This additional day occurs in years that are multiples of four, i.e., 1992, 1996, 2000, 2004, 2008, 2012.
Logic for Leap Year
Most of us definitely know the logic behind checking for a leap year in a Gregorian calendar. These are the three rules that should be followed to identify a leap year.
- The year can be evenly divided by 4,
- If the year can be evenly divided by 100, it is NOT a leap year, except if;
- The year is also evenly divisible by 400. Then it is a leap year.
Program to check leap year in C
In the given C program, we ask the user to enter a year and assign it to a variable. Next, we use the if else statements and implement the above logic to check whether the entered year is a leap year or not.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if evenly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year
// If the year is evenly divided by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// otherwise year is not leap year
else {
printf("%d is not a leap year.", year);
}
return 0;
}
Output of the above code-
Enter a year: 2020
2020 is a leap year.
Enter a year: 2021
2021 is not a leap year.
We can also write the above program as-
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;
}
Output of the above code:
Enter a year: 2022
2022 is not a leap year
C program to check leap year between given range
In the given C program, we will find the leap years between the given range of two years.
#include <stdio.h>
#include <conio.h>
void main(){
int i;
int min_year,max_year;
printf("Enter the lowest year: ");
scanf("%d",&min_year);
printf("Enter the highest year: ");
scanf("%d",&max_year);
printf("Leap years in given range are: ");
for(i = min_year;i <= max_year; i++){
if(((i%4==0)&&(i%100!=0))||(i%400==0))
printf("%d ",i);
}
getch();
}
Output of the above code-
Enter the lowest year: 1990
Enter the highest year: 2020
Leap years in given range are: 1992 1996 2000 2004 2008 2012 2016 2020
Related Articles
Prime factors of a number in cArmstrong number program in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to check whether a number is positive or negative
Priority queue using array in C
C program to calculate compound interest
Bit stuffing program in C
C program for employee salary calculation
C program to search an element in an array