How To Implement Leap Year Program in C?

Last updated on Sep 26,2020 9.4K Views

Leap Year Program in C

If you are a programmer and have attended interviews, you would know that there many programming interviews that may have a question to Program  a code to find out a leap year. This tricky question which seems simple bothers many. In this article we would be discussing how to implement a leap year program in C.

Table of Content

Let us start this article by understanding what is a leap year first

What Is Leap Year?

Leap year occurs every once in 4 years. It is the year which has 366 days rather than 365. We can write a simple if else code to find if the year is a leap year or not.

C Code: Finding A Leap Year In C

#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap yearn");
scanf("%d", &year);
if(year%400 == 0)
printf("%d is a leap year.n", year);
else if(year%100 == 0)
printf("%d isn't a leap year.n", year);
else if (year%4 == 0)
printf("%d is a leap year.n", year);
else
printf("%d isn't a leap year.n", year);
return 0;
}

Output:

If it is a leap year:

Output - Leap Year Program In C - Edureka

 

 

 

 

 

 

 

 

If it is not a leap year,

Output - leap year in c - Edureka

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Explanation

In the code above, we take input from the user. Then we check a list of conditions. We first check if the year entered by the user and is divisible by 400.  This is done to check if the year is exactly divisible by 400 e.g. 1600, 2000,4000,2400,etc. 

Next, if the year is not divisible by 400 then check if the year is divisible by 100. If it is not divisible by 400 and is divisible by 100 then it is not a leap year, for example 1900, 2100,2200,1800,1300.

If the year is not divisible by 400 or 100 then, check if it is exactly divisible by 4, if yes, it is a leap year. For example, 2016,2020,2024,2032,2048 etc.Last, if it is not divisible by 4,400 or 100 it isn’t a leap year. For example, 2021, 2018, 2019,2022,2015 etc. This is exactly what is done in the conditional statements above.

These are the 4 conditions to check for to check whether if it is a leap year or no.

With this we come to the end of this blog on ‘Leap Year Program In C’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  this blog and we will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.