Skip to main content

Check leap year

Short answer

A year is leap if divisible by 4 but not 100, or divisible by 400. Check condition and print result.

Write a program to find whether a year is leap or not.

A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. Hence 2024 and 2000 are leap years; 1900 and 2023 are not.

#include <stdio.h>
int main(void)
{
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);
    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;
}

Output for 2024:

Enter a year: 2024 is a leap year

Other test results: 2000 is a leap year, 1900 is not, 2023 is not.

Explanation

  • year % 4 == 0 && year % 100 != 0 covers ordinary leap years (2024).
  • year % 400 == 0 covers century years such as 2000; 1900 is divisible by 100 but not by 400, so it is not leap.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 5 questions →