Leap year c++: In the previous article, we have discussed about C++ Program to Find Largest Number Among Three Numbers. Let us learn how to Check Leap Year in C++ Program.
Methods to check leap year in c++
In this article, we will check whether a year is a leap year or not using c++. For a year to be a leap year it must satisfy the following condition:-
- The year is multiple of 400.
- The year is multiple of 4 and not multiple of 100.
Now let see the different method to write the above condition in c++ and check whether a year is a leap year or not.
Method 1-Using nested if-else statements
In this method, we use the nested if-else statement to check whether a leap year is a leap year or not. First, we will write the code for this and then understand how the code run.
#include <iostream> using namespace std; int main() { int year=2016; if (year % 4 == 0) { if (year % 100 ==0) { if (year % 400 == 0) cout << year << " is a leap year."; else cout << year << " is not a leap year."; } else cout << year << " is a leap year."; } else cout << year << " is not a leap year."; return 0; }
Output
2016 is a leap year.
Explanation:
First, we check if the year is divisible by 4 or not. If it is not divisible, then it is not a leap year. If it is divisible by 4, then we use an inner if statement to check whether a year is divisible by 100. If it is not divisible by 100, it is still divisible by 4 and so it is a leap year. We know that the century years do not leap years unless they are divisible by 400. So, if the year is divisible by 100, another inner if statement checks whether it is divisible by 400 or not. Depending on the result of that innermost if statement, the program determines whether a year is a leap year or not.
Method 2-Using logical and( && ) and logical or( || ) operator
Here we can also use logical operators to check whether a year is a leap year or not.This will reduce the line of code and might be easy to understand. Let rite a code for this.
#include <iostream> using namespace std; bool checkYear(int year) { return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } int main() { int year=2016; checkYear(year)?cout << year << " is a leap year.":cout << year << " is not a leap year."; return 0; }
Output
2016 is a leap year.
Explanation
We know that logical and( && ) return 1 if both the statements on which we applied logical and(&&) gives 1 else it return 0. In case of logical or( || ) return 1 if both the statements on which we applied logical or( || ) gives 1 or any one of the statement gives 1 else it return 0. So we use this logic to write the program.
First, we apply logical and(&&) on year%4 and year%100. For its result to be 1 we have to ensure that both return 1. So this statement ensures that for the year to be a leap year it must be divided by 4 and not by 100. If it returns 0 then we simply say that year is not a leap year else we check that if the year is divided by 400 or not. If yes then we say that year is a leap year otherwise year is not a leap year.
So these are the methods to check year is a leap year or not in c++.
C++ Codes list contains the general functions, nested loops, statements, libraries. Beginners can get the important codes list of C++ programming language from this page easily.