You are here

Leap year program in C

C program to check leap year: C code to check if a year is a leap year or not. To understand the logic of the program read Leap year article at Wikipedia. This code is based on the Gregorian Calendar.

C program to check leap year

  1.  #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int year;
  6.  
  7.   printf("Enter a year to check if it is a leap year\n");
  8.   scanf("%d", &year);
  9.  
  10.   if (year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000
  11.     printf("%d is a leap year.\n", year);
  12.   else if (year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900, 2100
  13.     printf("%d isn't a leap year.\n", year);
  14.   else if (year%4 == 0) // Exactly divisible by 4 and neither by 100 nor 400 e.g. 2020
  15.     printf("%d is a leap year.\n", year);
  16.   else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019
  17.     printf("%d isn't a leap year.\n", year);  
  18.    
  19.   return 0;
  20. }

Output of program:
Leap year C program output

Download Leap year program.