Leap Year Program In C (5 Different Ways)
In this article, we are going to write a Leap Year Program in C to check whether a given year is Leap Year or Not.
But first of all, you need to know what is a leap year..? So let’s first know what is a leap year, then we will learn to make a program to generate a leap year.
What is Leap year?
Generally, there are total 365 days in a year but there are total 366 days in Leap year and leap year comes every 4 years. Below I am telling some points so that you can know the difference between leap year and normal year.
- Leap year is a year which is different from the normal year.
- Leap year comes once in 4 years. The month of February in leap year is of 29 days instead of 28 days .
- The following are some examples of leap years – 1600, 1988, 1992, 1996, 2000, 2016 and 2020.
- Although 1700, 1800, and 1900 are century years, they are not leap years because there are some different conditions for being a leap year. Let us know about these conditions.
The condition given below is used to check whether a year is a leap year or not.
- If a year is divided by 4, 100 and 400, then that year is a leap year.
- If a year is divisible by 4 by 100 and not by 400, then that year is a leap year.
- If a year is not divisible by 4 then that year is not a leap year.
- If a year is divisible by 4 and 100 but not divisible by 400, then that year is also not a leap year.
So out of the above given conditions, you should remember more of the first two conditions because using these two conditions, we are going to write a program to check leap year.
We are going to make two types of programs to get out of Leap Year. In the first program, we will enter any one year from the user and check whether that year is a leap year or not, and in the second program, we will enter two years from the user and check how many leap years are between those two years.
We will make this program in various ways. These are as follows:
- C Program to Check Leap Year using if else
- Write A C Program to Check leap years between the range of two years
- C Program to Check Leap Year using function
- C Program to Check Leap Year using conditional operator
- C Program to Check Leap Year using switch case
To make this program, we will use the following concept given below.
If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given above.
Now without wasting time let’s start programming.
Write A Leap Year Program in C
Algorithm
- Program Start
- Declaration of variable
- Enter year to check leap year or not
- Check condition
- Give answer according to condition
- Program End
Program
//Write A C Program to Check Leap Year Using if else
#include<stdio.h>
void main()
{
int year;
printf("Enter The Year\n");
scanf("%d",&year);
if(year%100==0)
{ if(year%400==0)
printf("Leap year");
else
printf("Not a Leap year");
}
else
{ if(year%4==0)
printf("Leap year");
else
printf("Not a Leap year");
}
}
Output
Enter The Year
2019
Not a Leap year
Write A C Program to Check Leap Years Between The Range of Two Years
Program
//Write A C Program to Check Leap Years Between The Range of Two Years
#include<stdio.h>
void main()
{
int startYear, endYear, i;
printf ("Enter starting years: ");
scanf ("%d", &startYear);
printf ("Enter ending years: ");
scanf ("%d", &endYear);
printf ("Leap Years between %d to %d are: \n", startYear, endYear);
for (i= startYear; i<=endYear; i++)
{
if(((i% 4== 0) && (i % 100 != 0)) || (i% 400 == 0))
{
printf("%d \n",i);
}
}
}
Output
Enter starting years: 2000
Enter ending years: 2022
Leap Years between 2000 to 2022 are:
2000
2004
2008
2012
2016
2020
C Program to Check Leap Year Using Function
To Check Leap Year Using Function, first you need to know about the function. You can read about the function from the link given below.
Program
//C Program to Check Leap Year using function
#include<stdio.h>
void leapyear(int year)
{
if(year%100==0)
{ if(year%400==0)
printf("Leap year");
else
printf("Not a Leap year");
}
else
{ if(year%4==0)
printf("Leap year");
else
printf("Not a Leap year");
}
}
void main()
{
int year;
printf("Enter The Year\n");
scanf("%d",&year);
//Calling Function to Check Leap Year
leapyear(year);
}
Output
Enter The Year
2020
Leap Year
C Program to Check Leap Year Using Conditional Operator
To make and understand this programs well, first of all you need to know about conditional operator. You can read about the conditional operator in detail by clicking on the link given below.
Program
//C Program to Check Leap Year Using Conditional Operator
#include<stdio.h>
void main()
{
int year;
printf("Enter the Year :\n");
scanf("%d",&year);
(year%100==0)?(year%400==0)? printf("Leap year"): printf("Not a Leap year") :(year%4==0)? printf("Leap year"): printf("Not a Leap year");
}
Output
Enter the Year
2022
Not a Leap Year
C Program to Check Leap Year Using Switch case
To make and understand these programs well, first of all you need to know about Switch case statement. You can read about the Switch case statement. in detail by clicking on the link given below.
Program
//C Program to Check Leap Year Using Switch Case Statement
#include<stdio.h>
void main()
{
int year,y;
printf("Enter the Year :\n");
scanf("%d",&year);
y = year % 400 == 0 || year % 100 == 0 || year % 4 == 0;
switch (y) {
case 1:
printf("\n%d is the leap year.\n", year);
break;
case 0:
printf("\n%d is not the leap year.\n", year);
break;
default:
printf("\n%d is not the leap year.\n", year);
}
}
Output
Enter the Year
2020
2020 is the leap year
Read More -:
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
So friends in this article We will make Leap Year Program in C in various ways. These are as follows:
- C Program to Check Leap Year using if else
- Write A C Program to Check leap years between the range of two years
- C Program to Check Leap Year using function
- C Program to Check Leap Year using conditional operator
- C Program to Check Leap Year using switch case
If you have difficulty to understanding any program or have any doubts or questions, then tell me in the comment below.