Leap Year Checker in C

 

 

Leap Year Checker in C

This HTML document provides a C program to check if a given year is a leap year. The program is explained in detail, including its structure and functionality.

Program Code


#include <stdio.h>

// Function to check if a year is a leap year
int isLeapYear(int year) {
    // Leap year is divisible by 4
    // but not divisible by 100, except if divisible by 400
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        return 1; // True (leap year)
    } else {
        return 0; // False (not a leap year)
    }
}

int main() {
    int year;

    // Input year from user
    printf("Enter a year: ");
    scanf("%d", &year);

    // Check if the year is a leap year
    if (isLeapYear(year)) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }

    return 0;
}
    

Program Structure

The program is structured as follows:

  • Include Header Files: #include <stdio.h> is included to enable input and output functions.
  • Function Definition: The isLeapYear function checks if a given year is a leap year based on the rules of the Gregorian calendar:
    • A year is a leap year if it is divisible by 4 and not divisible by 100, except if it is divisible by 400.
  • Main Function:
    • The main function starts by prompting the user to enter a year.
    • The entered year is read and stored in the year variable.
    • The isLeapYear function is called with the entered year to determine if it is a leap year.
    • Based on the result, a message is printed indicating whether the year is a leap year or not.

Documentation

Function isLeapYear:

  • Input: Integer year – The year to be checked.
  • Output: Integer – Returns 1 if the year is a leap year, otherwise returns 0.
  • Description: Determines if the provided year is a leap year based on the leap year rules.

Function main:

  • Input: None directly, but reads an integer input from the user.
  • Output: Prints whether the entered year is a leap year or not.
  • Description: Manages user input, calls the isLeapYear function, and prints the result.

 

Leave a Reply

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