C Program to Check Leap Year

Updated on December 4, 2024
Check leap year header image

Introduction

A leap year, occurring every four years, adds an extra day in the calendar, making the year 366 days long instead of the typical 365 days. This adjustment aligns the calendar year with the astronomical year. In programming, determining whether a given year is a leap year or not can be a foundational exercise for beginners, particularly in languages like C. This routine calculation combines basic arithmetic and conditional logic, central components of any programming language.

In this article, you will learn how to create a C program to check if a year is a leap year. Discover how to apply arithmetic operations and condition checking through comprehensive examples that encapsulate best practices and clear logical structuring.

Leap Year Determination in C

Basic Leap Year Conditions

To determine whether a year is a leap year, follow these general rules:

  1. The year must be divisible by 4;
  2. If the year can also be divided by 100, it must additionally be divisible by 400.

This means that any year that is divisible by 4 is a leap year unless it can be evenly divided by 100, in which case it must also be divisible by 400.

Example Program to Check Leap Year

Here is a simple example in C that utilizes these conditions to determine if a given year is a leap year or not.

  1. Start by including the necessary header files and declare the main function:

    c
    #include <stdio.h>
    
    int main() {
        int year;
        printf("Enter a year: ");
        scanf("%d", &year);
    
  2. Implement the leap year checking logic:

    c
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            printf("%d is a leap year.\n", year);
        } else {
            printf("%d is not a leap year.\n", year);
        }
        return 0;
    }
    

    In this code, you first ask the user to enter a year. Following that, the if statement checks the conditions for a leap year:

    • It first checks if the year is divisible by 4 and not divisible by 100.
    • Alternatively, it checks if the year is divisible by 400. If either condition is satisfied, the program prints that the year is a leap year. Otherwise, it prints that it's not a leap year.

Conclusion

Using a C program to check for leap years is an excellent exercise to understand conditional statements and logical operations. The provided example demonstrates these principles clearly, involving checks for multiple conditions to determine leap years. Implement this simple program and try extending it by allowing continuous checks without restarting the program, enhancing both your coding skills and understanding of leap year calculations.