C Program to Check Given Year is Leap Year.

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.
Example:

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:

Step 1: Input the year from the user.
Step 2: Check if the year is evenly divisible by 4.
Step 3: If it is not divisible by 4, it is not a leap year.
Step 4: If it is divisible by 4, check if it is also divisible by 100.
Step 5: If it is divisible by 100, check if it is also divisible by 400.
Step 6: If it is divisible by 400, it is a leap year.
Step 7: If it is divisible by 100 but not by 400, it is not a leap year.
Step 8: If it is divisible by 4 and not by 100, it is a leap year.

C Code:
Below is the C program to check whether the year is a leap year or not:

//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;
}
Output:
Enter a year: 2024
2024 is a leap year.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS