In this article, you will learn how to write a C program to check leap year. But let’s first understand what is a leap year before writing a C program to check whether a given year is a leap year or not.
What is a Leap Year?
A leap year is a calendar year that contains an additional day. It has 366 days instead of 365, by extending February to 29 days rather than the common 28. A leap year comes in every 4 years.
For example – 2004, 2008, 2012, 2016, 2020, etc.
You can easily check whether a given year is a leap or not using the C program. A year is a leap year if the following conditions match:
- If the given year is a multiple of 400. That means divided by 400.
- If the given year is a multiple of 4 but not a multiple of 100. That means divided by 4 but not divided by 100.
You just need to check the above condition for the leap year.
Algorithm to find leap year:
Following is the pseudo-code to find a leap year.
Algorithm: IF year MODULER 400 IS 0 THEN leap year ELSE IF year MODULER 100 IS 0 THEN not_leap_year ELSE IF year MODULER 4 IS 0 THEN leap_year ELSE not_leap_year
Explanation:
- If the given year is divisible by 400 then the first “if” statement executes and marks it leap year.
- If the first given year is not divisible by 400 then, the next else-if condition is checked, and if the given year is divisible by 100, then mark it as not a leap year.
- If the again else-if statement is not executed that means the given year is not divisible by 100 then, the next else-if condition is checked, and if the given year is divisible by 4, then mark it as a leap year.
- If none of the above is true then the else part executes and marks it as not a leap year.
Flow Chart to check Leap Year:
The below chart is describing the above-discussed pseudo-code to find the leap year.
C Program to check leap year:
Below we are writing some example programs that are describing how we can check leap year using the C language. So let us see the code,
Method 1:- Using an if-else statement
This is an implementation of the above pseudo-code.
#include <stdio.h> int main() { unsigned int year = 0; printf("Enter a valid year:"); scanf("%u",&year); if((year%400 == 0) || ((year%4 == 0) && (year%100!=0))) { printf("%u is a leap year\n", year); } else { printf("%u is not a leap year\n", year); } return 0; }
Output-1:
Enter a valid year:2020 2020 is a leap year
Output-2:
Enter a valid year:2001 2001 is not a leap year
Code Analysis:
In the above c code example, first, we ask the users to enter the year.
printf("Enter a valid year:"); scanf("%u",&year);
Now check the primary condition for a leap year. If the given year satisfies the condition of a leap year then the given year is a leap year.
if((year%400 == 0) || ((year%4 == 0) && (year%100!=0))) { printf("%u is a leap year\n", year); } else { printf("%u is not a leap year\n", year); }
For example 2020. The year 2020 is divisible by 4 but not by 100. So, it is a leap year. But, the year 2001 is neither divisible by 4 nor by 400. So, it is not a leap year.
Method 2:- Using the if-else-if ladder
Another approach to checking leap year using the if-else-if statement.
#include <stdio.h> int main() { unsigned int year=0; printf("Enter a valid year:"); scanf("%u",&year); if(((year%4) == 0) && ((year%100)!=0)) { printf("%u is a Leap Year.\n\n\n",year); } else if(year%400 == 0) { printf("%u is a Leap Year.\n\n\n",year); } else { printf("%u is Not a Leap Year.\n\n\n",year); } return 0; }
Method 3:- Using the nested if-else
The third approach uses the nested if-else statement.
#include <stdio.h> int main() { unsigned int year=0; printf("Enter a valid year:"); scanf("%u",&year); if ((year%4) == 0) { if ((year%100) == 0) { if ((year%400) == 0) { printf("%u is a Leap Year.\n\n\n",year); } else { printf("%u is Not a Leap Year.\n\n\n",year); } } else { printf("%u is a Leap Year.\n\n\n",year); } } else { printf("%u is Not a Leap Year.\n\n\n",year); } return 0; }
Method 4:- Using Conditional Operator (ternary operator)
Here we check the leap year using the conditional operator.
#include <stdio.h> int main() { unsigned int year=0; printf("Enter the year:"); scanf("%u",&year); //check leap year using conditional operator int isLeapYear = ((year%400) != 0); isLeapYear = isLeapYear? (((year%4) == 0) && ((year%100) != 0)):1; if(isLeapYear) { printf("%u is a Leap Year.\n\n\n",year); } else { printf("%u is Not a Leap Year.\n\n\n",year); } return 0; }
Method 5:- Using a function
In this method, we create a function to find a leap year. It makes the code more readable. I think it is the best approach to check the leap year. In function, you can use any described method to check leap year. I am describing some ways to create a function to check leap year but there is “N way” you can create as per your requirement.
First Way:-
#include <stdio.h> int isLeapYear(unsigned int year) { int result = 0; if ((year%4) == 0) { if ((year%100) == 0) { if ((year%400) == 0) { result = 1; } } else { result = 1; } } return result; } int main() { unsigned int year=0; int leapYear = 0; printf("Enter the year:"); scanf("%u",&year); //check leap year leapYear = isLeapYear(year); if(leapYear) { printf("%u is a Leap Year.\n\n\n",year); } else { printf("%u is Not a Leap Year.\n\n\n",year); } return 0; }
Output:
Enter the year:2010 2010 is Not a Leap Year.
Code Analysis:
In the above c code example, first, we ask the users to enter the year.
printf("Enter the year:"); scanf("%u",&year);
Now check the leap year to call the function isLeapYear(). If the year is leap year function returns 1 otherwise returns 0.
leapYear = isLeapYear(year); if(leapYear) { printf("%u is a Leap Year.\n\n\n",year); } else { printf("%u is Not a Leap Year.\n\n\n",year); }
Second Way:
#include <stdio.h> //Return 1 if year is leap year int isLeapYear(unsigned int year) { int ret = ((year%400) != 0); ret = ret? (((year%4) == 0) && ((year%100) != 0)):1; return ret; } int main() { unsigned int year=0; int leapYear = 0; //Ask to enter year printf("Enter the year:"); scanf("%u",&year); //check leap year leapYear = isLeapYear(year); if(leapYear) { printf("%u is a Leap Year.\n\n\n",year); } else { printf("%u is Not a Leap Year.\n\n\n",year); } return 0; }
C Program to Find Leap Years Within a Given Range:
In the previous examples, you learn how to check year is a leap year or not. But now I am asking to find leap years between the given two years. That means you need to find the leap years to the given range of years.
Don’t worry I am not only asking the question I am also writing the solution but I suppose you should try first.
#include <stdio.h> //function to find leap year int isLeapYear(unsigned int year) { int result = 0; if ((year%4) == 0) { if ((year%100) == 0) { if ((year%400) == 0) { result = 1; } } else { result = 1; } } return result; } int main() { int startYear, endYear; int year = 0; printf("Enter the Valid Beginning year of the range: "); scanf("%d",&startYear); printf("Enter the Valid Ending year of the range: "); scanf("%d",&endYear); /* Now find the leap year between the starting and ending year. */ printf("Leap Years between %d and %d are: \n", startYear, endYear); for (year= startYear; year<= endYear; ++year) { const int leapYearFound = isLeapYear(year); if (leapYearFound) { printf("%d \n", year); } } return 0; }
Output:
Enter the Valid Beginning year of the range: 1991 Enter the Valid Ending year of the range: 2022 Leap Years between 1991 and 2022 are: 1992 1996 2000 2004 2008 2012 2016 2020
Recommended Post:
- Check date validity in C?
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- Count of Leap Years in a given year range.
- Using the C program to check valid date (date is valid or not)
- Check expiry date Using C program
- C program to print day name of week
- Convert number of days in terms of Years, Weeks and Days using C program
- C program to find century for a year
- C program for Binary Search.
- How to use if in C programming.
- How to use C if-else condition?
- How to use for loop in C?
- You should know while loop use.
- When we should use do while in the C program.
- Operators with Precedence and Associativity.
- How to pass an array as a parameter?
- Memory Layout in C.
- File handling in C, In a few hours.
- Replacing nested switches with the multi-dimensional array
- How to access a two-dimensional array using pointers?
- Brief Introduction of switch case in C.
- 100 C Interview Questions.