English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C program to determine if it is a leap year

Comprehensive Collection of C Programming Examples

In this example, you will learn how to check if the year entered by the user is a leap year.

To understand this example, you should be familiar with the followingC programmingTopic:

a leap year can be divided by4is divisible by, but ends with 00, except for century years that are divisible by400 is divisible by, a century year is a leap year only.

For example,

  • 1999is not a leap year

  • 2000 is a leap year

  • 2004is a leap year

Program to check for a leap year

#include <stdio.h>
int main() {
   int year;
   printf("Enter year: ");
   scanf("%d", &year);
   //is divisible by400 is divisible by to be a leap year
   if (year %% 400 == 0) {
      printf("%d is a leap year.", year);
   }
    //If it is10a multiple of 0
    //but is not divisible by400 is divisible by
   else if (year %% 1000 == 0) {
      printf("%d is not a leap year.", year);
   }
    //a leap year, if it is not divisible by10is divisible by 0
    //but is divisible by4is divisible by
   else if (year %% 4 == 0) {
      printf("%d is a leap year.", year);
   }
   //Other cases, it is not a leap year
   else {
      printf("%d is not a leap year.", year);
   }
   return 0;
}

Output1

Enter year: 1900
1900 is not a leap year.

Output2

Enter year: 2012
2012 It is a leap year.

Output3

Enter year: 2020
2020 is a leap year.

Comprehensive Collection of C Programming Examples