Checking whether a year is a leap year is a common task in C programming when working with date and time calculations. A leap year is a year with 366 days instead of 365. This happens because the Earth takes a little more than 365 days to go around the sun. To keep the calendar accurate, we add one extra day to February every four years, making it 29 days long. In this article, we will discuss what a leap year is, rules to find a leap year, pseudocode for a leap year in C, and finding a leap year in C using if-else, while loop, switch statement, function, and ternary operator.
Table of Contents:
What is a Leap Year?
A year that has one extra day than a normal year, which has 365 days, is a leap year. A leap year has 366 days. This extra day is added to February. It occurs once every four years.
According to the Gregorian calendar, a solar year is approximately 365.25 days, and that is treated as a normal year. The extra 0.25 days of every year accumulate over the four years and add up to one full day, and the year becomes 366 days.
Rules to Find a Leap Year
To check whether a year is a leap year or not, you must follow these rules.
A year is a leap year if:
- It is divisible by 4 but not by 100.
- It is not divisible by 100, and divisible by 400.
You can check this table for more clearer understanding:
Condition |
Result |
Not divisible by 4 |
Not a leap year |
Divisible by 4, not by 100 |
Leap year |
Divisible by 100, not by 400 |
Not a leap year |
Divisible by 400 |
Leap year |
Workflow Diagram of a Leap Year in C
The workflow diagram visually represents the logic to find a leap year in a C program.
Pseudocode for a Leap Year in C
START
INPUT year
IF (year is NOT divisible by 4)
PRINT "Not a leap year"
ELSE IF (year is NOT divisible by 100)
PRINT "Leap year"
ELSE IF (year is divisible by 400)
PRINT "Leap year"
ELSE
PRINT "Not a leap year"
END
Explanation:
- If the year is not divisible by 4, then it is a normal year.
- If the year is divisible by 4 but not by 100, then the year is a leap year.
- If the year is divisible by 100, then check again:
- If the year is also divisible by 400, then it is a leap year.
- Else the year is not a leap year.
C Program to Check Leap Year Using if-else Statement
Output:
The code shows how a year 2024 as an input is given in a C program to check whether the year is a leap year or not by using the if-else statement by using the divisibility rules, and then prints the result “2024 is a leap year” as an output to the console.
Get 100% Hike!
Master Most in Demand Skills Now!
C Program to Check Leap Year Using Ternary Operator
Output:
The code shows how a year 2025 as an input is given in a C program to check whether the year is a leap year or not, by using the ternary operator and the divisibility rules, and then prints the result “2025 is not a leap year” as an output to the console.
C Program to Check Leap Year Using a Function
Output:
The code shows how a function “isLeapYear()” is defined to find a leap year using the divisibility rules, which returns the result from the main() function, and then it is printed to the console.
C Program to Find Leap Year Using While Loop
Output:
The code shows how a year 2016 as an input is given in a C program to check whether the year is a leap year or not by using a while loop and the divisibility rules, and then prints the result “2016 is a leap year” as an output to the console.
C Program to Find Leap Year Using a Switch Statement
Output:
The code shows how a year 2019 as an input is given in a C program to check whether the year is a leap year or not by using a switch statement and the divisibility rules, and then prints the result “2019 is not a leap year” as an output to the console.
C Program to Find Leap Years Within a Given Range
Output:
The code shows how the leap years are found within a given range between 2000 and 2025 by using the isLeapYear() function, based on the leap year rules, and then the list of leap years is printed to the console.
Conclusion
Understanding a leap year is important to understand the calendar updates in the number of days than the usual calendar of a year. A leap can be found manually, too, with the help of divisibility rules. Different methods in C can be used to find a leap year easily. So, by understanding what a leap year is, and different methods in C to find a leap year, you can easily find a leap year with the help of the divisibility rule manually and technically.
C Program to Check Leap Year – FAQs
Q1. How do I manually check if a year is a leap year?
You can check if a year is a leap year manually: if the year is divisible by 4 and not divisible by 100, unless it is divisible by 400.
Q2. Is the year 2100 a leap year?
No, 2100 is not a leap year because it is divisible by 4 and 100, but it is not divisible by 400.
Q3. Can I use nested ternary operators to check leap years?
Yes, you can use nested ternary operators to check leap years, but you should use a simple if-else statement for more precise and less complex programming.
Q4. Which method is best for checking leap years in C?
The best method to find a leap year is to use a reusable function because it makes your code cleaner, modular, and easier to test.
Q5. How often does a leap year occur?
A leap year occurs every 4 years, except years divisible by 100 but not divisible by 400.