You are here

Leap year program in C

C program to check leap year: C code to check leap year, year will be entered by the user.

C program to check leap year

 
#include <stdio.h>
 
int main()
{
  int year;
 
  printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);
 
  if ( year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000
    printf("%d is a leap year.\n", year);
  else if ( year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900, 2100
    printf("%d is not a leap year.\n", year);
  else if ( year%4 == 0 ) // Exactly divisible by 4 and neither by 100 nor 400 e.g. 2016, 2020
    printf("%d is a leap year.\n", year);
  else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019
    printf("%d is not a leap year.\n", year);  
 
  return 0;
}

Download Leap year program.

Output of program:
Leap year C program output

To understand the logic of C program for leap year, read Leap year article at Wikipedia. This code is based on Gregorian Calendar.