Write a C program to check whether a year is a leap year or not

Description: Take a year as input. Print if it is a leap year or not.

Hint:

  • A year is a leap year if it is divisible by 400, or it is divisible by 4 but not by 100.

C program to check for a leap year

#include <stdio.h>

int main(void) {
    int year;

    printf("Enter a year: ");
    scanf("%d", &year);

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

Output:

Output of the above program is

Enter a year: 2024
2024 is a leap year

How it works

  • 2024 is divisible by 4 and not by 100, so it is a leap year.
  • 1900 is divisible by 100 but not by 400, so it is not. The year 2000 is divisible by 400, so it is.

Read C Operators for the logical operators.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.