Before we dive into the C program to determine if the user entered year is a leap year or not, let us first understand what a leap year is.
What is a Leap Year?
A leap year is a year that occurs once every four years. Based on the Gregorian calendar, the theory is that a year is around 365.25 days long. But since we consider a year to be 365 days, the additional 0.25 days are added to the next calendar year, which brings the total to 1 day in the fourth year. Hence, instead of 365 days, a leap year is 366 days long. The additional day is added as the 29th of February.
How to Find a Leap Year Using C?
To find whether a year is a leap year or not using a leap year C program, all you need to do is enter some conditions (mathematical) in the program code with the help of If… Else statement; Following are the conditions to check if the given year is a leap year or not:
- The entered year must be divisible by 4
- The entered year must be divisible by 400 but not by 100
The second condition is used to segregate the century years from leap years. Century years are the ones with the ’00s in the end, for instance, 1300, 1500, 1400 and likewise. A century year is considered to be a leap year only if it is evenly divisible by 400. For example, the years 1200, 1600, and 2000 are all century leap years since these numbers are perfectly divisible by 400.
Flow Diagram of a Leap Year C Program
Below is a flow diagram of how to write a leap year C program.
*[&& is logical AND-operator / II is logical OR-operator]
Pseudocode for a Leap Year C Program
You can write a pseudocode for the leap year C program as below:
Start procedure leap_year()
If year % 4 = 0 AND year % 100!= 0 OR year%400 == 0
Print leap year
Else
Print not leap year
End IF
End procedure
Also Read: What is C Programming?
How to Implement Leap Year Program in C?
We will now use the above pseudocode to write a leap year C program. After implementing the code, we will provide different years on each execution to see the results. Here’s how you can implement the code in C programming.
#include <stdio.h>
int main(){
int y;
printf("Enter the year to check: ");
scanf("%d",&y);
if (((y % 4 == 0) && (y % 100!= 0)) || (y%400 == 0))
printf("It is a leap year");
else
printf("It is not a leap year");
return 0;
}
Output 1:
It is a leap year as the year 2016 is divisible by 4 but not divisible by 100.
Output 2:
It is not a leap year as the year 2013 is not divisible by 4.
Output 3:
It is a leap year as the year 1200 is divisible by 400.
Output 4:
It is not a leap year as the year 1500 is divisible by 4 and 100, making it a century year.
C Program to Find Leap Years Within a Given Range
In the previous example, we asked the user to input a year and checked if it is a leap year or not. But here, we will write a leap year C program to ask the user to input a range and print all the leap years between the range.
#include <stdio.h>
int main(){
int start_Year, end_Year, i;
printf("Enter the starting year of the range: ");
scanf("%d",&start_Year);
printf("Enter the last year of the range: ");
scanf("%d",&end_Year);
//the given range
printf("Leap Years between %d and %d are: \n", start_Year, end_Year);
for (i= start_Year; i<= end_Year; i++){
if (((i % 4 == 0) && (i % 100!= 0)) || (i % 400 == 0)){
printf("%d \n", i);
}
}
return 0;
}
Output:
That was all about the leap year C program
Earn upto 25 CEUs from Caltech CTME and score a new job with an average annual package of 9-10 L after completing the PGP in Full Stack Web Development. Enroll Today!
Conclusion
You can use a similar concept to find out different ranges of years. The leap year C program is also sometimes used as an introduction point to the if-else statements in C. If you want to learn more about C fundamentals, such as pointers and loops, sign up on Simplilearn’s SkillUp platform. The platform provides a wide range of free online courses to help you clear the basics of multiple programming languages, including C.
Besides learning about C programming, you should also master some other popular programming languages because multinational companies prefer a full-stack developer in today’s competitive times. Hence, it is essential and recommended that you increase your knowledge, skills, and scope of work. You can do all of that by simply opting for Simplilearn’s Post Graduate Program in Full Stack Web Development Course. The course will help you get acquainted with a wide array of programming languages and relevant tools to help you pursue a software development career. So what are you waiting for? Get started now!