C++ Program to Check Leap Year or not
Program to check whether a year is a leap year or not
Here we will discuss how to check whether a year is a leap year or not using the C++ programming language.
In a year there are 365 days, but once every fourth year there are 366 days that year is called a leap year.
Leap year comes in every four year, how to know that the year is a leap year or not, there are two ways to know,
- First is, All those years which are divided by 4 are leap years, except century years.
- Second is,if a century year is divisible by 400 then the year is a leap year.
Let’s see how the code works
- User gives an input.
- The input is stored in an int type variable say year.
- year is checked for being a leap year or not with the following condition
if( ((year % 4 == 0)&&(year % 100 != 0)) || (year % 400==0) )
- If the above condition is true then input is a leap year otherwise input is not a leap year.
//Leap year or not
using namespace std;
cout<<"Enter year to check: ";
//checking for leap year
if( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400==0) )
//input is a leap year
cout<<year<<" is a leap year";
//input is not a leap year
cout<<year<< " is not a leap year";
Enter year to check: 2019
2019 is not a leap year