21/06/2021

Leap Year Program in C with Logic, Explanation and Output

Write a Leap Year Program in C to check whether the year is Leap Year or Not, using IF-ELSE Statements. The user must enter the Year and Program should Prompt the message "The Entered Year is Leap Year or Not". Leap years have total 366 numbers of days and a February 29. If we need to find which year is Leap Year or not.

You can modify this code to find the next or previous Leap Year if the User Entered the current year or you can also find the list of all the Leap Years in a Century. Download the Leap Years List | List of All Leap Years From 1800 to 3000.

Leap Year Examples:
  1. 1992: Leap Year
  2. 2002: Not a Leap Year
  3. 2016: Leap Year
  4. 2100: Not a Leap Year

Leap Year Program in C Using IF-ELSE


#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 is Not a Leap Year.", year);
        } else
            printf("%d is a Leap Year.", year);
    } 
    else
        printf("%d is Not a Leap Year.", year);
    return 0;
}

The output of the Leap Year Program in C

Output of Leap Year Program in C

Leap Year Program Explanation Step by Step


To Understand this Leap Year Program in C, we have to go with the Line Line Code. So In this, there are the following steps we are going to explain everything in detail.

  1. Step 1: Take Input from the User, and Store the user value in a variable(As we have stored in a year)
  2. Step 2: Now the real logic begins, Here the year is divided by then 4 then it's a Leap year, Because as we all know that Leap Year comes every 4 years which is the reason we are going to divide by 4. if (year % 4 == 0), Else print the entered year is not a leap year.
  3. Step 3: Again We are using Nested IF-ELSE because we have to check for century year(Ex. 1900, 2200, 2300, 2500), Because the Century year is easily divided by 4. So if (year % 100 == 0) this condition is true then we will go to the fourth step. Else print Not a Leap Year.
  4. Step 4: Now if this if (year % 400 == 0) condition is true then the Year Entered by the user is a Leap Year, and if this condition is not true that means the year is not a Leap Year.

Leap Year Condition

if (year % 4 == 0) 
{
    if (year % 100 == 0) 
	{
        if (year % 400 == 0)
           printf("%d is a Leap Year.", year);
        else
           printf("%d is Not a Leap Year.", year);
     } else
        printf("%d is a Leap Year.", year);
} 
else
printf("%d is Not a Leap Year.", year);
Check This:
 C++ Program To Check Year Is Leap Year Or Not

List of Leap Years: from 1900 to 3000


List of Leap Years from 1900 to 3000

Note: 2012 is missing out List. 2012 is a Leap Year.
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

1 comment: