C Leap Year Check Program


A Leap Year Check program determines whether a given year is a leap year or not. A leap year has 366 days instead of the usual 365 days and occurs every four years to help synchronize the calendar year with the astronomical year.

Rules for Identifying a Leap Year:

  1. A year is a leap year if it is divisible by 4.
  2. However, if the year is divisible by 100, it is not a leap year unless:
  3. The year is also divisible by 400, in which case it is a leap year.

Logic of the Program:

  1. Input a year from the user.
  2. Check the conditions for leap year:
    • If the year is divisible by 4.
    • If it is divisible by 100, check if it is also divisible by 400.
  3. Print whether the year is a leap year or not based on these conditions.

Program:

Here’s a C program to check if a year is a leap year:

#include <stdio.h> int main() { int year; // Input a year from the user printf("Enter a year: "); scanf("%d", &year); // Check if the year is a leap 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; }

Explanation:

  1. Variables:

    • int year: Holds the year inputted by the user.
  2. Input:

    • The program prompts the user to enter a year.
  3. Leap Year Logic:

    • The program checks if the year is divisible by 4 and not divisible by 100, or if it is divisible by 400.
    • If either condition is met, the program considers it a leap year.
  4. Output:

    • The program prints whether the entered year is a leap year or not based on the evaluation.

Sample Output:

Example 1:

Enter a year: 2024 2024 is a leap year.

Example 2:

Enter a year: 1900 1900 is not a leap year.

Example 3:

Enter a year: 2000 2000 is a leap year.

Key Points:

  • Divisibility Conditions: The program effectively uses logical operators to combine conditions for determining leap years, ensuring accurate results.
  • User Interaction: The program interacts with the user by prompting for input and displaying results clearly.
  • Efficiency: The checks are simple and efficient, with only a few modulus operations needed to determine if the year is a leap year.
  • Comprehensive Handling: The program correctly handles all cases of leap years as per the Gregorian calendar rules.