C Program to check leap year
In this C Programming example, we will implement the program to check leap year using control statements, functions and print the output on the screen.
1. What is a Leap Year?
A Leap Year is a calendar year that contains an additional day in the shortest month i.e. February has 29 days.
Fun Fact: Earth takes approximately 365.2425 days to orbit the Sun which is a solar year. But we consider a calendar year to be 365 days. And to cover this error almost every fourth year we add a day in the year. That is a leap year.
We can check whether the year is a leap year or not using the following conditions
- If the year is divisible by 100 and 400 then it is considered a leap year.
- If the year is divisible by 4 but not by 100, then also the year is considered as the leap year.
Example: 2000, 2024, 2028, 2036..etc. These are all Leap Years. Example: 1800, 1900, 2100 ..etc. These are all not Leap Years because they are divisible by 100 but not by 400.
Helpful topics to understand this program better are-
2. C Program to check Leap year
Let’s discuss the execution(kind of pseudocode) for the program to check leap year in C.
- Initially, the program will prompt the user to enter a year.
- The program will then call for
checkForLeapYear()function. - In this function, the year entered by the user will then be passed and the divisibility for the year to be a leap year or not is verified.
- A year is a leap year if any of the conditions
year % 4 == 0 && year % 100 != 0oryear % 100 == 0 && year % 400 == 0is true. - If the year entered is found to be a leap year then it is printed onto the console otherwise, It is not a leap a year is printed onto the console.
Let us now implement the above execution of the program to check leap year in C.
#include <stdio.h>
void checkForLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0) {
printf("%d is a Leap Year", year);
} else if (year % 100 == 0 && year % 400 == 0) {
printf("%d is a Leap Year", year);
} else {
printf("%d is not a Leap Year", year);
}
}
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
checkForLeapYear(year);
return 0;
}Output Enter year: 2024 2024 is a Leap Year
Output 2 Enter year: 1900 2024 is not a Leap Year
3. Conclusion
In this C Programming example, we have discussed what is a leap year and why do we have a leap year and we have also implemented the program to check leap year.
Helpful Links
Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.
Also for the example C programs please refer to C Programming Examples.
All examples are hosted on Github.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?