C Program to Check if a Year is a Leap Year or Not

In this article, we will write a C program to check if a year is a leap year or not. The program prompts the user to enter a year, checks if it is a leap year or not, and prints the result on the output window.

Example:

Enter a year: 2024
Year 2024 is a Leap year

Enter a year: 2021
Year 2021 is not a Leap year

Enter a year: 1900
Year 1900 is not a Leap year

Before diving into the implementation of the program, we first need to understand what is a leap year.

A leap year is a year that contains an extra day in the month of February i.e. February 29th. So instead of having 365 days, a leap year has 366 days. A leap year occurs once every four years. For example, the years 2004, 2008, and 2012, are all leap years.

To check if a year is a leap year or not, we generally assume that if the year is divisible by 4 then it’s a leap year otherwise not.

But that’s not 100% true. For example, the year 1900 is divisible by 4 but it’s not a leap year. So, when we are checking leap year dividing by 4, we also need to check that it must not be divisible by 100.

To cover all scenarios you have to use the following algorithm:

  • If the year is divisible by 4 but not divisible by 100, it’s a leap year.
  • Otherwise, check if the year is divisible by 400. If yes, it’s a leap year.

See implementation in the following program:

// C program to check if a year
// is a leap year or not

#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("Year %d is a Leap year", year);
  } else {
    printf("Year %d is not a Leap year", year);
  }

  return 0;

}

Output 1:

Enter a year: 2024
Year 2024 is a Leap year

Output 2:

Enter a year: 2021
Year 2021 is not a Leap year

Output 3:

Enter a year: 1900
Year 1900 is not a Leap year

Output 4:

Enter a year: 2000
Year 2000 is a Leap year

As you can see from the above output, the program covers all scenarios, whether it’s a century or a normal year.

That’s all for this article. Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts