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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)