You are here

Leap year program in C

C program to check leap year: C code to check leap year, the year will be entered by a user. To understand the logic of C program for leap year, read Leap year article at Wikipedia. This code is based on Gregorian Calendar.

C program to check leap year

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

Output of program:
Leap year C program output

Download Leap year program.