Leap Year Program in C using If-Else Statement
ADVERTISEMENTS
Leap year program in C using if else statement.
In this article, you will learn how to check the leap year program in C using the if-else statement.
Example
----Enter the year----
2008
2008 is the leap year.
Samples of the leap years
2000, 2004, ....., 2016, and 2020 are the leap years
Source Code
// Leap Year Program in C using If else Statement
#include <stdio.h>
int main() {
int y;
// y - Value of the year
printf("----Enter the year----\n");
scanf("%d", &y);
if (y % 400 == 0) {
printf("\n%d is the leap year.\n", y);
} else if (y % 100 == 0) {
printf("\n%d is not the leap year.\n", y);
} else if (y % 4 == 0) {
printf("\n%d is the leap year.\n", y);
} else {
printf("\n%d is not the leap year.\n", y);
}
return 0;
}
Output
----Enter the year----
2008
2008 is the leap year.
Explanation
In the given program, we have taken input 2008
from the user via the system console. Then It will find the remainder of the input value by using these values 400, 100, 4
, If the remainder of the input value comes 0
using these values than the input value is leap year otherwise It's not a leap year.
- y = input % 400, if y = 0 then leap year otherwise It's not a leap year
- y = input % 100, if y = 0 then leap year otherwise It's not a leap year
- y = input % 4, if y = 0 then leap year otherwise It's not a leap year
Leap Year Program in C using Switch Case Statement
// Leap Year Program in C using Switch Case Statement
#include <stdio.h>
int main() {
int y, r;
// y - Value of the year
printf("----Enter the year----\n");
scanf("%d", &y);
r = y % 400 == 0 || y % 100 == 0 || y % 4 == 0;
switch (r) {
case 1:
printf("\n%d is the leap year.\n", y);
break;
case 0:
printf("\n%d is not the leap year.\n", y);
break;
default:
printf("\n%d is not the leap year.\n", y);
}
return 0;
}
Output
----Enter the year----
1999
1999 is not the leap year.