#include <stdio.h>

int main() 
{
    int year;

    // Asking the user to input a year
    printf("Enter a year: ");
    scanf("%d", &year);

    // Leap year condition
    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;
}

Explanation:

Returning 0: We return 0 at the end of the program to indicate that the program has finished executing successfully.cCopy codereturn 0;

Including the standard input-output library: We include the stdio.h header file so that we can use printf for displaying output and scanf for taking input from the user.cCopy code#include <stdio.h>

Main function: The program execution starts from the main() function.cCopy codeint main() { ... }

Declaring a variable: We declare an integer variable year to store the year that the user will input.cCopy codeint year;

User input: We use printf to prompt the user to enter a year and then use scanf to capture that year and store it in the variable year.cCopy codeprintf("Enter a year: "); scanf("%d", &year);

Checking for a leap year:

  • A leap year occurs:
    • If a year is divisible by 4 but not divisible by 100, or
    • If a year is divisible by 400.
  • This means:
    • Years like 1996, 2004, and 2020 are leap years because they are divisible by 4 and not by 100.
    • Years like 1900 are not leap years because they are divisible by 100 but not by 400.
    • However, years like 2000 are leap years because they are divisible by 400.
  • The condition ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) handles this logic.
cCopy codeif ((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); }

Output

Enter a year: 2020
2020 is a leap year.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights