C Program to check if a year is Leap year or not


March 18, 2022, Learn eTutorial
1321

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

In this c program, we need to find whether the given year is a leap year or not.

What is a leap year?

A leap year has three hundred and sixty-six days, which is added to February, which has only twenty-eight days. So in a leap year, it will have twenty-nine days that happen once in four years. In this c program, we need to calculate the given year is a leap year or not.

The logic of this c program is to divide the year by four and check the remainder. If the remainder is zero, it is a leap year. For example, let us check the year '2000' which is divisible by four, so it is a leap year. Here we have to use the mod operator to divide the given year by 4, and if the remainder is zero, it is a leap year. Here simple library functions like if-else are used. Syntax of 'if-else' is:


if (testExpression) {

  // codes inside the body of if

} else {

  // codes inside the body of else

}

If the test expression is true, we will execute the code in the if condition and skip the else part. If the test expression is false, we will run the else part and ignore the if condition statements. The source code to calculate whether the given year is a leap year or not is:

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the Integer Variable 'year'.

STEP 3: Accept a Year from the user to check it is a Leap Year or not by using the scanf function.

STEP 4: Using if condition check 'year % 4 = 0' then display the year is a Leap Year.

STEP 5: Else display the year is not a Leap Year.

C Source Code

                                          #include <stdio.h>

void main() {
  int year;
  printf("Enter a year\n"); /* user inputs the year */
  scanf("%d", & year);
  if ((year % 4) == 0)
    printf("%d is a leap year", year); /* using mod operator to check the given year is leap year on not */
  else
    printf("%d is not a leap year\n", year);
}
                                      

OUTPUT

Enter a year
2000
2000 is a  leap year

RUN2

Enter a year
2007
2007 is not a leap year