Complementary task for topic: 2

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

If/else II.

If/else II.

Task: Determine if a year is a leap year:
Write a C program that prompts the user to enter a year and determines if it is a leap year or not using if/else statements. Display the result using printf().

Hint: If the year is divisible by 400, it is considered a leap year. If the year is divisible by 100 but not by 400, it is not a leap year. If the year is divisible by 4 but not by 100, it is considered a leap year. Otherwise, it is not a leap year.

Solution
#include 

int main() {
    int year;
    
    printf("Enter a year: ");
    scanf("%d", &year);
    
    if (year % 400 == 0) {
        printf("The year %d is a leap year.\n", year);
    } else if (year % 100 == 0) {
        printf("The year %d is not a leap year.\n", year);
    } else if (year % 4 == 0) {
        printf("The year %d is a leap year.\n", year);
    } else {
        printf("The year %d is not a leap year.\n", year);
    }
    
    return 0;
}



Explanation
In this example, the program uses printf() to prompt the user to enter a year. The scanf() function is used to scan the input from the user and store it in the year variable. The program uses if/else statements to check the conditions for leap years.
< < previous    next > >