Leap Year Programs in C
This article will demonstrate Leap Year Programs using If Else Statement, While Loop, Conditional Operator, Switch Statement Using Functions.
A leap year is a calendar year that contains an additional day (February 29) to keep the calendar year synchronized with the solar year or astronomical year. This article will teach us the various methods used to write leap-year programs in C. We will be covering the following sections:
- What is a Leap Year?
- Using If Else Statement
- Using While Loop
- Using Conditional Operator
- Using Switch Statement
- Using Functions
- Endnotes
So, without further ado, let’s get started!
What is a Leap Year?
The leap year is necessary to correct the mismatch between the calendar year of 365 days and the time it takes the Earth to complete its orbit around the sun, which is about 365.25 days. This is done by adding an extra day, February 29, to the calendar every four years. This means that the year 2000 was a leap year, but 1900 was not. The next leap year is 2024.
The rules for determining if a year is a leap year are as follows:
- If a year is evenly divisible by 4, 100, and 400, then it is a leap year.
- If a year is divisible by 4 but not by 100 and not divisible by 400, then it is also a leap year.
- If a year is not divisible by 4, then it is not a leap year.
- If a year is divisible by 4 and 100 but not by 400, then it is not a leap year.
When creating a program, it is mandatory to check the first two conditions to check for a leap year. In C programming, we can determine a leap year using the following methods:
- If Else Statement
- While Loop
- Conditional Operator
- Switch Statement
- Functions
A leap year program can be written in two ways:
- Take a year as input from the user and determine if it is a leap year.
- Take two years as input from the user (range), and calculate the number of leap years between that range.
Let’s see how we implement both programs using the abovementioned methods.
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
Using If Else Statement
Example 1:
Take a year as input from the user and determine whether it is a leap year or not using the if-else statement in C:
#include <stdio.h> int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d is a leap year.", year);
} else {
printf("%d is not a leap year.", year);
}
return 0;
}
Output:
In this program, the user is prompted to enter a year. The value entered is stored in the variable year. Then, the if-else statement checks if the year is evenly divisible by 4, not by 100, or by 400. If it is, it’s a leap year; otherwise, it’s not. The program then prints the result.
Example 2:
Take two years as input from the user and calculate the number of leap years within that range using the if statement in C:
#include <stdio.h> int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
while(1) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d is a leap year.", year);
break;
} else {
printf("%d is not a leap year.", year);
break;
}
}
return 0;
}
Output:
In this program, the user is prompted to enter two years. The values entered are stored in the variables year1 and year2. Then, the program uses a for loop to check each year between year1 and year2. Within the loop, an if statement checks if the current year is divisible by 4, not divisible by 100, or divisible by 400. If it is, it is a leap year, and the leap_years variable is incremented. Finally, the program prints the leap years between the two years.
Using While Loop
Example 1:
Take a year as input from the user and determine whether it is a leap year or not using the while loop in C:
#include <stdio.h> int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
while(1) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d is a leap year.", year);
break;
} else {
printf("%d is not a leap year.", year);
break;
}
}
return 0;
}
Output:
In this program, the user is prompted to enter a year. The value entered is stored in the variable year. Then, the program uses a while loop with a condition of 1(true), which will run forever until a break statement is encountered. Within the loop, an if-else statement checks if the current year is divisible by 4, not by 100, or by 400. If it is, then it is a leap year; otherwise, not a leap year, and the respective result is printed. The loop then breaks, and the program ends.
Example 2:
#include <stdio.h> int main() {
int year1, year2, i, leap_years = 0;
printf("Enter the starting year: ");
scanf("%d", &year1);
printf("Enter the ending year: ");
scanf("%d", &year2);
i = year1;
while(i <= year2) {
if((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
leap_years++;
}
i++;
}
printf("Number of leap years between %d and %d is %d", year1, year2, leap_years);
return 0;
}
Take two years as input from the user and calculate the number of leap years within that range using while loop in C:
Using Conditional Operator
Example 1:
#include <stdio.h> int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? printf("%d is a leap year.", year) : printf("%d is not a leap year.", year);
return 0;
}
Take a year as input from the user and determine whether it is a leap year or not using a conditional operator in C:
Output:
In this program, the user is prompted to enter a year. The value entered is stored in the variable year. Then, the program uses a conditional operator that checks if the year is divisible by 4, not divisible by 100, or divisible by 400. If it is, then it is a leap year, and it will execute the first statement after the? Operator, otherwise not a leap year, will execute the statement after the: operator.
Example 2:
Take two years as input from the user and calculate the number of leap years within that range using the conditional operator in C:
#include <stdio.h> int main() {
int year1, year2, i, leap_years = 0;
printf("Enter the starting year: ");
scanf("%d", &year1);
printf("Enter the ending year: ");
scanf("%d", &year2);
for (i = year1; i <= year2; i++) {
(i % 4 == 0 && i % 100 != 0) || i % 400 == 0 ? leap_years++ : leap_years;
}
printf("Number of leap years between %d and %d is %d", year1, year2, leap_years);
return 0;
}
Output:
In this program, the user is prompted to enter two years. The values entered are stored in the variables year1 and year2. Then, the program uses a for loop to check each year between year1 and year2. Within the loop, the program uses a conditional operator that checks if the current year is divisible by 4, not by 100, or by 400. If it is, then it is a leap year, and the leap_years variable is incremented using the first statement after the? Operator, otherwise leap_years variable is not modified using the statement after the: operator. Finally, the program prints the leap years between the two years.
Using Switch Statement
Example 1:
#include <stdio.h>
int main() {
int year, y;
printf("Enter the year: ");
scanf("%d", &year);
y = year % 400 == 0 || year % 100 == 0 || year % 4 == 0;
switch (y) {
case 1:
printf("%d is a leap year.\n", year);
break;
case 0:
printf("%d is not a leap year.\n", year);
break;
default:
printf("Invalid input.\n");
}
return 0;
}
Take a year as input from the user and determine whether it is a leap year or not using the switch statement in C:
Output:
In this program, the user is prompted to enter a year. The value entered is stored in the variable year. Then, the program uses a variable y, which is set to 1 if the year is a leap year or 0 if it’s not. The program then uses a switch statement to check the value of y, and prints the appropriate message, whether the year is a leap year or not.
Example 2:
Take two years as input from the user and calculate the number of leap years within that range using the switch statement in C:
#include <stdio.h> int main()
{
int startYear, endYear, leapYears = 0;
printf("Enter start year: ");
scanf("%d", &startYear);
printf("Enter end year: ");
scanf("%d", &endYear);
for (int year = startYear; year <= endYear; year++)
{
switch(year % 4)
{
case 0:
if (year % 100 == 0)
{
if (year % 400 == 0)
{
leapYears++;
}
}
else
{
leapYears++;
}
break;
default:
break;
}
}
printf("Number of leap years between %d and %d: %d\n", startYear, endYear, leapYears);
return 0;
}
Output:
This program prompts the user to enter a start year and an end year and then uses a for loop to iterate through each year within that range. The program uses a switch statement for each year to check whether the year is a leap year. A year is considered a leap year if it is divisible by 4, except for end-of-century years, which must be divisible by 400. The program uses this logic to determine whether each year is a leap year, and if so, increments the leapYears counter. Finally, the program prints out the total number of leap years within the given range.
Using Functions
Example 1:
#include <stdio.h> // Function to check if a year is a leap year int isLeapYear(int year) {
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 1;
}
}
else
{
return 0;
}
}
int main()
{ int year;
printf("Enter a year: ");
scanf("%d", &year);
if (isLeapYear(year))
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
return 0;
}
Take a year as input from the user and determine whether it is a leap year or not using functions in C:
Output:
This program prompts the user to enter a year, then calls the function isLeapYear(), passing the entered year as input. A year is considered a leap year if it is divisible by 4, except for end-of-century years, which must be divisible by 400. The program uses this logic to determine whether a year is a leap year. If it is a leap year, it returns 1. If not, it returns 0. Then, the main Function checks the return value of the Function. If the return value is 1 it means the entered year is leap year otherwise, not. It prints the result accordingly.
Example 2:
Take two years as input from the user and calculate the number of leap years within that range using functions in C:
#include <stdio.h> // Function to check if a year is a leap year
int isLeapYear(int year)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 1;
}
}
else
{
return 0;
}
}
// Function to count the number of leap years in a given range
int countLeapYears(int startYear, int endYear)
{
int leapYears = 0;
for (int year = startYear; year <= endYear; year++)
{
if (isLeapYear(year))
{
leapYears++;
}
}
return leapYears;
} int main()
{
int startYear, endYear;
printf("Enter start year: ");
scanf("%d", &startYear);
printf("Enter end year: ");
scanf("%d", &endYear);
int leapYears = countLeapYears(startYear, endYear);
printf("Number of leap years between %d and %d: %d\n", startYear, endYear, leapYears);
return 0;
}
Output:
This program prompts the user to enter a start year and an end year, then it calls the function countLeapYears(), passing the entered start and end year as an input. The function countLeapYears() uses a for loop to iterate through each year within the given range, and it calls the function isLeapYear(), passing the current year as an input.
The function isLeapYear() checks whether the year is a leap year, and if it is, it returns 1, if not it returns 0. The countLeapYears() function uses the return value of isLeapYear() to determine whether the current year is a leap year and if so, it increments the leapYears counter. Finally, the main Function prints out the total number of leap years within the given range.
This is a more structured and organized way to calculate the number of leap years within a range.
Endnotes
This article was useful in helping you learn how to check for a leap year in C programming using various methods. Explore our C articles to learn more about the language and consolidate your knowledge of the fundamentals.
Contributed by Prerna Singh
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio