In this C program, we are going to check whether the given year is a leap year or not. But before writing code for this we first need to understand what is Leap Year? A leap year is a year that contains an extra day, February 29, making it 366 days instead of the usual 365 days.
What is Leap Year?
Leap years occur once in four years to keep the calendar year synchronized with the astronomical year, which is approximately 365.2422 days long. Usually, a non-leap year contains 28 days in the month of February but a leap year contains 29 days in the month of February.
Below are a few rules to check if the given year is a leap year or not:
- The year must be evenly divisible by 4.
- If the year is divisible by 100, it must also be divisible by 400.
Input: 2008 Output: 2008 is a Leap Year Explanation:
- Check if the year is divisible by 4: 2008 % 4 = 0.
- Since 2008 is divisible by 4, proceed to the next step.
- Check if the year is divisible by 100: 2008 % 100 = 8.
- 2008 is not divisible by 100, so not necessary to check its divisibility by 400.
- Since 2008 is divisible by 4 and not by 100, it is a leap year.
Algorithm to Check Leap Year:
//C program implementation to check leap year #include <stdio.h> int main() { int year; printf("Enter a year: "); scanf("%d", &year); //check leap year conditions if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { printf("%d is a leap year.\n", year); } else { printf("%d is not a leap year.\n", year); } } else { printf("%d is a leap year.\n", year); } } else { printf("%d is not a leap year.\n", year); } return 0; }
Enter a year: 2024
2024 is a leap year.
No comments:
Post a Comment