C++ Program to Check the year is Leap year or not

January 12, 2023, Learn eTutorial
1415

What is a leap year?

In an ordinary year, we count all the days in a calendar from January to December, we would count 365 days. But approximately every four years, there will be an extra day for the year and it will be added to February, so a leap year has 366 days. A year that has 366 days with an extra day in February (29 days, usually 28 days) is called a LEAP YEAR. A leap year is coming after 3 normal years ie., the 4th year after a leap year will be the other one. Here, we are explaining how to check whether the given year is a leap or not using C++.

How can we check whether a given year is a leap year or not?

We can check for leap year just by checking if that is perfectly divisible by 4 (leap year has 366 days that is divisible by 4) except the century years. Years ending with 00 are called century years. To check the century years, we check if it is divisible by 400.

Steps to check for the leap year is given below

1. If the year is evenly divisible by 400, go to 4
2. If the year is evenly divisible by 100, go to 5
3. If the year is evenly divisible by 4, go to 4
4. Leap year
5. Not a leap year.

How can we check for a leap year in C++?

To check for leap year in C++, We Start the program with the main function. Declare an integer type variable year. Ask the user to enter an input. Here we are using the `if……else `statement to perform the checking operation.
If we need to choose between more than two alternatives, then we use the` if…..else if….else `statement. This is called the `if....else` ladder.

Here, if condition 1 is true then the code block 1 will execute. If condition 1 is false, then condition 2 is evaluated. If condition 2 is true then code block 2 is executed. If condition 2 is false then code block 3 will execute. There can be more than one else if statement but only one if and else statement is allowed.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the main function; int main().

Step 4: Declare an integer type variable ‘year’.

Step 5: Print a message “Enter a year”.

Step 6: Read the value to the variable ‘year’.

Step 7 `: `if ( year % 400 = = 0 ) then print leap year; else check if (year 0 = = 0 ) then Print not a leap year.

Step 8 :   Else if (year % 4 = =0 ) then print Print leap  year else case Print not leap year.

Step 9 : Exit the program.

C++ Source Code

```                                          ```#include <iostream>
using namespace std;

int main() {

int year;
cout << "Enter a year: ";
cin >> year;

// leap year if perfectly divisible by 400
if (year % 400 == 0) {
cout << year << " is a leap year.";
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
cout << year << " is not a leap year.";
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
cout << year << " is a leap year.";
}
// all other years are not leap years
else {
cout << year << " is not a leap year.";
}

return 0;
}```
```

OUTPUT

```Enter a year: 2022
2022 is not a leap year.```

VIEW ALL
VIEW ALL