Do you want to write a c program to check the leap year? Well, there is more than one way to do so. Leap year is a special occasion that occurs every fourth consecutive year.Â
A leap year is followed by one extra day as compared to the normal days in a year, i,e 366 days in a leap year. In this article, let us write a c program to check leap year.
What makes up a Leap Year?
A normal calendar year has only 365 days while it takes approximately 365.25 days for Earth to orbit around the sun i,e a solar year. We round off the days to 365 days and adjust one day to our calendar every four years. That makes a Leap Year.
A leap year contains one additional day in February month i,e. February 29. Our problem statement is to write a leap year program in C.
How to Develop a Logic to Find a Leap Year?
The first step in writing a program is to develop the logic behind it. In this problem statement, we have to develop a program to find whether a given year is a leap year or not. A given year can only be a leap year whenÂ
- If a given year is a multiple of 4 but not 100Â
- If a given year is a multiple of 400
For example, 2020 is a leap year while 2019 is not a leap year.Â
Also, Check some Basics C Programs for Beginners
C Program to Check Leap Year using If Statement
Let us write a C program to check leap year using the if statement. You can enter any value (year) of your choice and will get whether the given year is a leap year or not.Â
#include <stdio.h>
int main () { int year; printf(“\n Please enter a year of your choice \n”); scanf( “%d”, &year); if ((year%400 ==0)||(year%4==0)&&(year%100!=0)) } else printf (“%d is not a leap year.\n”, year); return 0; } |
Output
Explanation
With this C program, you can check whether a given year is a leap year or not. We are using a logical OR (||) and logical And (&&) expression to check for leap year. These logical OR and And operators help us check for multiple conditions within one if statement.Â
The first condition checks (year%4==0) whether the remainder of this expression is 0 or not. If the condition comes false then it will exit the condition and give output as “Not a leap year.” If the given year is a multiple of 100, then it is not a leap year.Â
Both the conditions, are i,e (year%4 ==0) )and (year%100! ==0), must hold to be a leap year. If the (year%400 == 0) this statement evaluates to 1, then the year is a leap year.
C Program to check Leap Year using Else if StatementÂ
This program allows users to check for a leap year using the else if statement. You can use multiple else if and else statements to find leap years. Check whether the year you use is a leap year or not.Â
#include <stdio.h>
int main () { int year; printf(” Please enter a year of your choice \n”); scanf( “%d”, &year); if (year%400==0) else if (year%100 ==0) else if (year%4 == 0) else return 0; } |
Output
Explanation
In this method, we will first check whether (year%400 == 0) is true. If it is true then the given year is a leap year. Otherwise, we will check whether (year%100==0), if it is true then the given year is not a leap year. At last, we check whether (year%4 == 0); if this is true then the given year is a leap year. And if no condition is satisfied, then the given year is not a leap year.Â
C Program to check Leap Year using Nested If Statement
You can also find whether a given year is a leap year or not using the nested If statement. We will use nested if to verify our logic and then produce the output.Â
#include <stdio.h>
int main () { int year; printf( “Please enter a year of your choice \n”); scanf( “%d”, &year); if (year%4 == 0) if (year%400 == 0) printf (“%d is a leap year.”, year); else printf (“%d is not a leap year.”, year); printf (“%d is a leap year.”, year); printf (“%d is not a leap year.”, year); return 0; } |
Output
Explanation
In this method, we use nested if statements to find whether a given year is a leap year or not. In the first statement, we check whether the given (year%4==0) is true or not. If the expression is true, then we will move to the next condition, however, if it is false, then the year is not a leap year.Â
In the second statement, we check whether (year%100) is false. If it is false, then the given year is a leap year, and if it is true, then we have to check whether the given year is divisible by 400 or not.
In the third statement, we check whether the given number is divisible by 400 or not. If the remainder of (year%400 == 0) is false, then the given number is definitely not a leap year.Â
Learn DSA with C++ with PW Skills
Are you looking for a career in software development, web development, or a similar profile? If you are choosing C++ language then PW Skills is here to help you master Data Structures with C++ programming. You will learn the basics and fundamentals of C++ programming and develop your skills.Â
You will be able to solve competitive programming and work on real-time projects. This is a self paced course covered by expert mentors. Hurry! Start your career with PW Skills.
Enroll in our Decode DSA with C++ and master competitive programming and Data Structure.Â
C Program to Check leap year FAQs
Q1. How to find the leap year in C?
Ans: We can find whether a given year is a leap year or not using the If statement, else if statement or nested if statement. We can use any of the above methods to find leap years.
Q2. How to check if a year is a leap year?
Ans: A given year is a leap year when it is divisible by 4. Also, century years like 2000, 1900 must be divided by 400 to be a leap year. For example, the year 2016 is divisible by 4 and hence is a leap year. 2000 is divisible by 400 and hence a leap year.
Q3. If a given year is divisible by 100 a leap year?
Ans: No, if a given year gives the remainder 0 divisible by 100 then it is not a leap year.
Q4. What is the algorithm to check for a leap year?
Ans: If a year divisible by 100 is only a leap year when it is also divisible by 400. Also, if a year is divisible by 4 then it is a leap year.