C

Leap Year Program in C

In this tutorial, we are going to see how to write a C program to check if the input year (entered by the user) is a leap year or not.

You can check if a year is a leap year or not using this mathematical logic:
 
Leap Year:

  • If a year is divisible by 4, 100 and 400, it is a leap year.
  • If a year is divisible by 4 but not by 100, it is a leap year.

Not a leap year:

  • If a year is not divisible by 4, it is not a leap year.
  • If a year is divisible by 4 and 100 but not by 400, it is not a leap year.
 

C Program to Check if a Year is a Leap Year

In this program, the user is prompted to enter a year. Then the program checks if the entered year is a leap year or not.

#include <stdio.h>

int main()
{
    int year;

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

    if(year % 4 == 0)
    {
        if( year % 100 == 0)
        {
            if ( year % 400 == 0)
                printf("%d is a leap year", year);
            else
                printf("%d not a leap year", year);
        }
        else
            printf("%d is a leap year", year );
    }
    else
        printf("%d not a leap year", year);

    return 0;
}

Output:

Enter a year: 2004
2004 is a leap year
  1. The user must first enter the year to be checked.
  2. The if statement checks if the year is a multiple of 4 but not a multiple of 100 or if it is a multiple of 400 (because not every year, a multiple of 4 is a leap year).
  3. The result is then displayed.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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