C Leap Year Program: Code & Explanation

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

    return 0;
}

In this code snippet, the user first inputs a year, and then uses conditional statements to determine if the year is a leap year. By the definition of a leap year, if a year is divisible by 4 but not by 100, or divisible by 400, then it is considered a leap year.

bannerAds