C++ Program to Check Leap Year

C++ Program to Check Leap Year

To understand this example, you should have the knowledge of the following C++ programming topics:


All years which are perfectly divisible by 4 are leap years except for century years (years ending with 00), which are leap years only if they are perfectly divisible by 400.

For example,

Leap Year Not Leap Year
1968 1971
2004 2006
2012 2010
1200 1700
1600 1800
2000 1900

In the examples below, the user is asked to enter a year, and the program checks whether the year entered by the user is a leap year or not.


Example 1: Check Leap Year Using if...else Ladder

#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 1

Enter a year: 1900
1900 is not a leap year.

Output 2

Enter a year: 2012
2012 is a leap year.

Example 2: Check Leap Year Using Nested if

#include <iostream>
using namespace std;

int main() {

  int year;

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

  if (year % 4 == 0) {
    if (year % 100 == 0) {
      if (year % 400 == 0) {
        cout << year << " is a leap year.";
      }
      else {
        cout << year << " is not a leap year.";
      }
    }
    else {
      cout << year << " is a leap year.";
    }
  }
  else {
    cout << year << " is not a leap year.";
  }

  return 0;
}

Here, we have used nested if statements to check whether the year given by the user is a leap year or not.

First, we check if year is divisible by 4 or not. If it is not divisible, then it is not a leap year.

If it is divisible by 4, then we use an inner if statement to check whether year is divisible by 100.

If it is not divisible by 100, it is still divisible by 4 and so it is a leap year.

We know that the century years are not leap years unless they are divisible by 400.

So, if year is divisible by 100, another inner if statement checks whether it is divisible by 400 or not.

If it's divisible by 400, it is a leap year. Otherwise, it's not a leap year.


Example 3: Check Leap Year Using Logical Operators

We can combine the conditions required for a leap year into a single if...else statement using the  && and || operators.

#include <iostream>
using namespace std;

int main() {

  int year;

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

  // if year is divisible by 4 AND not divisible by 100
  // OR if year is divisible by 400
  // then it is a leap year
  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
    cout << year << " is a leap year.";
  }
  else {
    cout << year << " is not a leap year.";
  }

  return 0;
}
Did you find this article helpful?