Algorithm
START Algorithm
- Step 1 → Take an integer variable of
year
- Step 2 → Assign a value to that variable
- Step 3 → Check if the year is
divisible by 4 but not 100, print "Leap Year"
- Step 4 → Check if the year is
divisible by 400, print "Leap Year"
- Step 5 → Else, print "Not Leap Year"
STOP Algorithm
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main() {
int year;
printf("Please Enter year to check leap year: ");
scanf("%d", &year); // Take input for year variable
if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) { // If Divisible by 4 and not 100 or Divisible by 400
printf("%d is a Leap Year", year);
} else {
printf("%d is not a Leap Year", year);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
2000
Output
Please Enter year to check leap year: 2000
2000 is a Leap Year
2000 is a Leap Year
#2 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) { //If year is divisible by 400, Leap year
printf("%d is a Leap year.", year);
} else if (year % 100 == 0) { // Not Leap year if divisible by 100 but not divisible by 400
printf("%d is not a Leap year.", year);
} else if (year % 4 == 0) { // leap year if not divisible by 100 but divisible by 4
printf("%d is a Leap year.", year);
} else {
printf("%d is not a Leap year.", year);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1999
Output
Enter a year:1999
1999 is not a Leap year
1999 is not a Leap year
#3 Get all leap years between a range
Code -
C Programming
#include<stdio.h>
int main() {
int startYear, endYear, year;
printf ("Enter a Start Year to check leap years: ");
scanf ("%d", &startYear);
printf ("Enter an End year to check leap years: ");
scanf ("%d", &endYear);
printf ("Leap Years between %d to %d are: \n", startYear, endYear);
for (year = startYear; year < = endYear; year++)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
printf("%d \n", year);
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
2000
2023
2023
Output
Enter a Start Year to check leap years: 2000
Enter an End year to check leap years: 2023
Leap Years between 2000 to 2023 are:
2000
2004
2008
2012
2016
2020
Enter an End year to check leap years: 2023
Leap Years between 2000 to 2023 are:
2000
2004
2008
2012
2016
2020
Demonstration
So, calculating leape year in c program is very easy. Let's demonstate the code -
- First, we'll take the input from the stdin.
- Then year will be leap year only if it is
divisible by 4, but not 100
- Or, if it is
divisible by 400, then leap year
- Otherwise all would be not leap year.