# Check Leap Year or not in C++

Hello Coders!! In this tutorial, we will learn how to implement the logic behind checking if the year given as input to the C++ program is a leap year or not. Before going into the program, we need to know what is a leap year and the logic behind it.

#### What is a Leap Year?

Leap Year is a Year that has an extra day compared to all the years. The extra day is February 29th. Usually, we have 365 days in a year but when the year is known to be a leap year then it consists of 366 days.

#### The logic behind the LEAP YEAR

For every four years, we get a leap year. For every 100 years, we skip a leap year unless the leap year is divisible by 400.

Therefore, a year can be known as a leap year if and only if any of the following conditions are true:

• The year must be divisible by 4 and not divisible by 100

[OR]

• The year is divisible by 4, 100, and 400.

### IMPLEMENTATION:

STEP 1: We need to include the required headerfiles and namespaces. we include the iostream headerfile as we use the cout, cin which are present in the iostream class.

#include <iostream>
using namespace std;

STEP 2: We start writing the main() function in which we write the logic to find if the year is a leap year or not. We declare an integer type of variable in which we store the input entered by the user. By using cout, we display a statement to the user stating that to enter a year value. The input entered by the user is received by using cin.

int yr;
cout<<"Enter any year of your choice : ";
cin>>yr;

STEP 3: We start implementing the logic in our program by using conditional statements.

If the year entered by the user is not divisible by 4 then the program displays that the year entered by the user is not a leap year. If the year entered by the user is divisible by 4 and not divisible by 100 then the year entered by the user is considered as a leap year. And if the year is divisible by 4, 100, and 400 then it displays that the year entered by the user is a leap year.

if(yr%4==0) {
if(yr%100!=0) {
cout<<"The year "<< yr <<" entered by the user is a leap year\n";
} else {
if(yr%100==0 && yr%400==0) {
cout<<"The year "<< yr <<" entered by the user is a leap year\n";
}
}
} else {
cout<<"The year "<< yr <<" entered by the user is not a leap year"<<endl;
}

### C++ program to check if a given year is leap year or not

The overall code is as follows:

#include <iostream>
using namespace std;
int main() {
int yr;
cout<<"Enter any year of your choice : ";
cin>>yr;
if(yr%4==0) {
if(yr%100!=0) {
cout<<"The year "<<yr<<" entered by the user is a leap year\n";
} else {
if(yr%100==0 && yr%400==0) {
cout<<"The year "<< yr <<" entered by the user is a leap year\n";
}
}
} else {
cout<<"The year "<< yr <<" entered by the user is not a leap year"<<endl;
}
}

#### OUTPUT 1:

Enter any year of your choice : 2020
The year 2020 entered by the user is a leap year

#### OUTPUT 2:

Enter any year of your choice : 2014
The year 2014 entered by the user is not a leap year