Whenever we have 366 days in a year instead of 365 days, that year is called a leap year. Leap year comes every fourth year. This adjusts the calendar year with the solar year.
In this tutorial, we will learn to write a C program to check whether a given year is a leap year or not.
Problem Statement:
Write a C program to check whether a year is a leap year or not.
Here our program will take integer input from the user.
It should print if the result on the screen. We will be using if-else statement to make this program.
Program
#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;
}
Example:
Input 1: 2000
Output:
2000 is a leap year
Input 2: 2001
Output:
2001 is not a leap year
Program Explanation:
- We have included stdio.h to enable the program to do input and output operations.
- The main() function is the entry point of any C program.
- Here, we have declared the variable ‘year’ of integer type to store user input.
- printf() function is used to show the output/message on the display.
- scanf() function is used to read the input year given by the user.
- if-else statement is used to apply check whether the given year is divisible by 4 and 100.
- The return statement returns 0 to the operating system and denotes the end of a program.
Conclusion:
In this blog, we have written a C program to check whether a year is a leap year or not using the if-else statement.
The above program takes a integer as input from the user.
And After taking input, with the help of if else statement it checks whether the number is divisible by 4 and 100 or divisble by 400.
I hope this blog was helpful to you in understanding the C program.