HomeC ProgrammingC Program to find if the Given Year is Leap Year or Not

C Program to find if the Given Year is Leap Year or Not

This C program determines whether a given year is a leap year or not. A leap year is a year that is exactly divisible by 4, except for years that are exactly divisible by 100. However, years that are divisible by 400 are leap years.

Problem statement

Write a C program to find if the given year is a leap year or not.

C Program to find if the Given Year is Leap Year or Not

#include <stdio.h>

int isLeapYear(int year) {
    if (year % 400 == 0) {
        return 1;
    } else if (year % 100 == 0) {
        return 0;
    } else if (year % 4 == 0) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    int year;

    printf("Leap Year Checker\n");
    printf("-----------------\n");

    printf("Enter a year: ");
    scanf("%d", &year);

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

    return 0;
}

How it works?

  1. The program starts by including the necessary header file stdio.h, which provides input/output functions.
  2. The function is LeapYear() takes an integer year as input and returns 1 if the year is a leap year, and 0 if it’s not.
  3. Inside the is LeapYear() function, it checks the following conditions:
    • If the year is divisible by 400, it is a leap year.
    • If the year is divisible by 100, it is not a leap year.
    • If the year is divisible by 4, it is a leap year.
    • If none of the above conditions are met, it is not a leap year.
  4. In the main() function:
    • It prompts the user to enter a year.
    • The input is stored in the year variable using the scanf() function.
    • It calls the isLeapYear() function with the year as an argument to determine if it’s a leap year or not.
    • Finally, it prints the result based on the returned value.

Input/Output

C Program to find if the Given Year is Leap Year or Not

Share:

Leave A Reply

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

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...