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:

#include<iostream>

using namespace std;

int main() {

  int year;

  //Taking user input for a year
  cout << "What year do you want to check? ";
  cin >> year;


   //Using single if statement to check all conditions

   //Checking if year can be divided by 4 or not divisible by 100 OR year is divisible by 400

   if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))

   cout<<year<<" is a leap year";

   //If above both criteria fails then its not a leap year

   else

   cout<<year<<" is not a leap year";

   return 0;

}

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.

#include <iostream>

using namespace std;

int main() {

  int year;

  //Taking user input for a year

  cout << "What year do you want to check? ";
  cin >> year;

  // It must be a leap year if divisible by 400 - so this is our first condition to check

  if (year % 400 == 0) {

    cout << year << " is a leap year.";

  }

  //if a year is not divisible by 400 but can be divided by 100 this is not a leap year e.e 1900, 1800

  else if (year % 100 == 0) {

    cout << year << " is not a leap year.";

  }

  // if both above conditions are false then we will check if the year can be divided by 4, if yeas then it's a leap year

  else if (year % 4 == 0) {

    cout << year << " is a leap year.";

  }

  // If all conditions are false, the entered year is not a leap year

  else {

    cout << year << " is not a leap year.";

  }




  return 0;

}

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.

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!