6/21/2021

Leap Year Program in C [Logic, Examples & Output] | IF-ELSE

 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.

Leap Year Examples:
  1. 1992: Leap Year
  2. 2002: Not a Leap Year
  3. 2016: Leap Year
  4. 2100: Not a Leap Year
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.

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;
}

Output

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 by Line Code. So In this, there are the following steps we are going to explain everything in details.

  1. Step 1: Take an 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 is the year is divided by then 4 then it's a Leap year, Because as we all know that Leap Year comes in every 4 years that 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 Century year easily divides by 4. So if 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 and 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
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

0 Comments: