Complementary task for topic: 4

M Nemeth · 2023-08-29 15:21:04.618220'

Functions: Check Leap Year

Functions: Check Leap Year

Write a C function is_leap_year that takes a year as input and returns 1 if it is a leap year, and 0 otherwise. A leap year is divisible by 4, but not divisible by 100, except when it is divisible by 400.

Hint:

Solution
#include 

// Function to check if a year is a leap year
int is_leap_year(int year) {
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        return 1;
    else
        return 0;
}

int main() {
    int year;
    printf("Enter a year to check if it is a leap year: ");
    scanf("%d", &year);

    if (is_leap_year(year))
        printf("%d is a leap year.\n", year);
    else
        printf("%d is not a leap year.\n", year);

    return 0;
}



Explanation

< < previous    next > >