Hit enter after type your search item

Check Whether Entered Year is Leap Year or Not in C++?

What is a leap year?

  • A leap year contains an additional day than other years.
  • The day is added to the month of February i.e. normally Feb is 28 days month while the leap year’s Feb is 29 days.
  • The leap year occurs once every four years.
  • However, if a year is divisible by 100 is not a leap year. For example, 1900 is divisible by 4 and also by 100 but it’s not a leap year.
  • Similarly, a year divisible by 400 is a leap year. For example, 2000 is divisible by 4 as well as 400 so, it was a leap year.

First Program – Using logical operator in the if statement to check leap year

In the first program, we used if..else statement to check if a year is a leap or not. The if statement uses a logical operator OR i.e. “||”.

A little explanation is also commented in the code below:


Sample Output as we entered year 1900:

CPP-leap-year-1

Not a leap year as it can be divided by 100.

Sample Output as we entered year 2000:

CPP-leap-year-2

Leap year as it can be divided by 400.

Sample Output as we entered year 2016:

CPP-leap-year-3

Leap year as it can be divided by 4 but not by 100.

Sample Output as we entered year 2017:

CPP-leap-year-2017

Not a leap year because it can’t be divided by 400, as well as 4.

C++ program to find if a year is leap year or not – if..else if ladder

We may find out if a year is leap or not by using different ways that handle all three conditions.

In the second program, we will use the modulus operator in if, else if…else statements to check if the entered year is leap or not.

A user is asked to enter the year to be and we will get it through all conditions.


Sample Output as we entered year 1900:

CPP-leap-year-1

Not a leap year as it can be divided by 100.

You may check other years by copying/pasting the code in your C++ IDE and executing the above programs.

 

This div height required for enabling the sticky sidebar