Signup/Sign In

Identify a Leap Year Program in C

Before we write the program to find a leap year in C language, let's understand how we can in general find which year is a leap year and then we will write a program for it.

What is a Leap Year?

For 1 rotation, Earth takes 24 hours. It takes approximately 365.25 days (365 days and 6 hours) for the Earth to complete one revolution around the sun. That is what we call a year. But since we only count 365 days in a year, we make up for it by adding those 6 hours for 4 years. We end up adding a day to a year every four years. This year is called a Leap year. In a leap year, February has 29 days instead of 28, so, in total, it has 366 days.

2017, 2018, 2019 are not leap years but 2020 is.

A year which is evenly divisible by 4 is said to be leap year. But this caused some errors because 1700, 1800, 1900, etc. are evenly divisible by 4 but not leap years.

To correct this mistake, in addition to being divisible by 4, if an year is divisible by 100, then it should also be divisible by 400, only then it will be a leap year.

Algorithm to find Leap Year

To check even divisibility of the number, we will use modulo operator(%). It results in zero if the number is evenly divisible by its quotient.

The algorithm for checking if the given year is a leap year or not is as follows:

  1. Take the user input.

  2. If the year is evenly divisible by 4, it maybe a leap year. Otherwise, it is not a leap year.

  3. If the year is divisible by 4, check if the year is evenly divisible by 100 too. If it is divisible by 4 and not by 100, then it is a leap year, otherwise, if it's divisble by 100 also, then we need to do one more check.

  4. If the year is divisble by both 4 and 100, we check if the year is evenly divisible by 400. If yes, then it is a leap year. Otherwise, it is not a leap year.

Program to identify whether the Input Year is a Leap Year or not

Now let's see the program to identify whether the input year is a leap year or not in C language.

#include<stdio.h>
int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int year;
    printf("Enter the year to check if it is a leap year: ");
    scanf("%d", &year);
    
    if(year % 4 == 0){
        if(year % 100 == 0){  
            if(year % 400 == 0)
                printf("\n\n%d is a leap year\n", year);
            else 
                printf("\n\n%d is not a leap year\n", year);
        }
        else
            printf("\n\n%d is a leap year\n", year);
    }
    else
        printf("\n\n%d is not a leap year\n", year);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

Program Output:

Identify Leap Year Program

Conclusion

In this tutorial, we learned what a leap year is and how do we identify a Leap Year program in C. To learn more such interesting C Programs, follow our complete C tutorial.