OverIQ.com

C Program to check whether a year is a leap year

Last updated on September 24, 2020


The following is a C program which checks whether the entered year is a leap year or not.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/******************************************************************
 * Program to check whether the entered year is a leap year or not
 ******************************************************************/

#include<stdio.h> // include stdio.h

int main() {
    int year;    

    printf("Enter a year: ");
    scanf("%d", &year);

    if( (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0) )
    {
        printf("%d is a leap year", year);
    }

    else
    {
        printf("%d is not a leap year", year);
    }

    return 0;
}

Try it now

Expected Output: 1st run:

1
2
Enter a year: 2000
2000 is a leap year

2nd run:

1
2
Enter a year: 1900
1900 is not a leap year

How it works #

To determine whether a year is a leap year or not, we use the following algorithm:

  1. Check if the year is divisible by 4. If it is, go to step 2, otherwise, go to step 3.
  2. Check if the year is divisible by 100. If it is, go to step 3, otherwise, the year is a leap year
  3. Check if the year is divisible by 400. If it is, then the year is a leap year, otherwise, the year is not a leap year.