Introduction to Leap Year Program in C
Generally, as we already know a year has 365 days but leap year consists of 366 days. This one day is added in the month of February. This month which generally has 28 days and also known as the shortest month in a year would get added with an extra day, which gives us a total of 29 days in that month. It is based on the Georgian calendar. Here, let us see how to write a program for checking out whether a given year is a leap year or not in C programming language.
Logic:
- Any year which is divisible for 4, and divisible by both 100 and 400 then it is a leap year.
- The year which is divisible by 4 and not divisible by 100 then that year would be a leap year.
- And obviously, if the year is divisible by 4 and 100 but not divisible by 400 then that year would not be called a leap year.
Pseudocode/Algorithm
Let us have the above-written logic in the form of a pseudo-code in an algorithmic written format:
- If the year is divisible for 400 alone. Then it is a leap year. If not, then it is a non-leap year.
- Else if the year given is only divisible by 100, then it is a non-leap year.
- Else if the same year is a leap year if the given year is completely divisible by 4.
Flowchart
Below, let us see the programming of leap year through a small flowchart:
The main condition lies with the year value of divisibility with 4 and 100 and the other condition is 400.
#include <stdio.h>
int main()
{
int r;
printf("Enter any year value: ");
scanf("%d",&r);
if( ( (r%4 == 0) && (r%100 !=0) ) || (r%400 == 0))
{
printf("This year is definitely a leap year");
}
else
{
printf("This year is not at all leap year");
}
}
Output:
We can have an output for the non-leap year too:
Above here, we have got all the conditions if the year values in a single if statement by using ‘and’ and ‘or’ statements.
Examples of Leap Program in C
Let us see the example below:
Example #1
#include <stdio.h>
int main()
{
int year=2020;
if(year % 4 == 0)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
{
printf("The given year 2020 is a leap year");
}
else
{
printf("The given year 2020 is not a leap year");
}
}
else
{
printf("The given year 2020 is a leap year");
}
}
else
{
printf("The given year 2020 is not a leap year");
}
}
Output:
- Here, the main and the first test would be, if the year given is being divisible by 4 or not.
- There is an if-else condition for the divisibility test for 4.
- If the condition gives the output true, then there would be next ‘if statements’ are true or not.
- If the divisibility test 100 condition becomes true then the test for 400 is being done.
- If that divisibility test is passed for 100 then the divisibility test for 400 should be done successfully too.
- Else, that year would not be considered as a leap year.
- And obviously, if the first step of divisibility by 4 has to be successful else at that point itself, we can consider that as a non-leap year condition.
Example #2
#include <stdio.h>
int main()
{
int y;
printf("Enter any year: ");
scanf("%d", &y);
if(y % 4 == 0)
{
if(y % 100 == 0)
{
if(y % 400 == 0)
{
printf("The year given is a leap year");
}
else
{
printf("The year given is not a leap year");
}
}
else
{
printf("This year given is a leap year");
}
}
else
{
printf("This year is not a leap year");
}
}
Output:
Another set of output can be as below:
Conclusion
So, this is how we can know whether any year is a leap year or not. We have our conditions of the divisibility test for 4, 100 and 400. We have actually analyzed and our coding is done using a single ‘if condition’ or multiple ‘if and else’ conditions. In either way, the same output can be obtained. In this way, we can get the leap year programming done in C programming language.
Recommended Articles
This is a guide to Leap Year Program in C. Here we discuss the introduction, Pseudocode/Algorithm, flowchart, and examples of leap year program in c. You may also have a look at the following articles to learn more –
- Factorial Program in C++
- Address Operator in C
- Leap Year Program in Java
- Leap Year Program in Python
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses