Select Page

# C++ Program to Check Leap Year (Using if-else)

by | Jan 19, 2023 | C++, C++ Programs

Home » C++ » C++ Programs » C++ Program to Check Leap Year (Using if-else)

## Introduction

In this program, you will learn to check the year entered is a leap year or not.

To understand this program, you must have gone through the following C++ topics

1. Operators

2. if-else statement

To check whether a year is a leap year or not, we have different situations.

All years completely divisible by 4 are leap years except for the values ending with 00.

The year values ending with 00, like 1700,1600, are leap years if perfectly divisible by 400.

## Program

```#include<iostream>
using namespace std;
int main()
{
int year;
cout<<"Enter a year";
cin>>year;

if(year%400==0) //if perfectly divisible by 400, it's a leap year
{
cout<<year<<" is a leap year";
}

else if(year%100==0) //if perfectly divisible by 100 but not by 400, it's not a leap year
{
cout<<year<<" is not a leap year";
}

else if(year%4==0) //if perfectly divisible by 4 but not by 100, it's a leap year
{
cout<<year<<" is a leap year";
}

else // neither condition written above states true, then it's not a leap year
{
cout<<year<<" is not a leap year";
}
return 0;
}```

## Explanation

In this program, the user is asked to enter a value that gets stored in the ‘year’ variable.

Four different statements are written using if, else if, and else. It is because the logic works in serial order as written in the above code.

The value entered by the user is 2018.

if(year%400==0) this gives false result with year=2018 thus, controls move to next statement.

else if(year%100==0), this gives a false result again with year=2018 thus, controls move to the next statement.

else if(year%4==0) 2018 is not completely divisible by 4, thus it is false.

Thus, the else part is executed, and ‘2018 is not a leap year is the output of the last ‘else’ statement.