Leap Year Program in C++

In this post, we will write a leap year program in the C++ language. A year is called leap year if the year is divisible by four, except for the years which are divisible by 100 but not divisible by 400. Therefore, the year 2000 was a leap year, but the years 1700, 1800, and 1900 were not.

The complete list of leap years in the first half of the 21st century is 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.

// C++ program to Check Leap year
#include<iostream>
using namespace std;
int main()
{
  // declare variable
  int year;

  // take year value
  cout << "Enter Year: ";
  cin >> year;

  // condition for leap year
  if ( (year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)) )
    cout << year << " is a Leap Year." << endl;
  else
    cout << year << " is not Leap Year" << endl;

  return 0;
}

Output:-

Enter Year: 2020
2020 is a Leap Year.

Enter Year: 2030
2030 is not Leap Year

Enter Year: 1900
1900 is not Leap Year

Leap Year in C++ Using Nested If-else

There are multiple ways to check if the year is a leap year or not. Here is the C++ program to check leap year using nested if-else statements.

// C++ program to Check Leap year
#include<iostream>
using namespace std;
int main()
{
  // declare variable
  int year;

  // take year value
  cout << "Enter Year: ";
  cin >> year;

  // check leap year
  if(year % 4 == 0)
  {
      if(year % 100 == 0)
      {
          if(year % 400 == 0)
            cout << "Leap Year" << endl;
          else
            cout << "Not a Leap Year" << endl;
      }
      else
        cout << "Leap Year" << endl;
  }
  else
    cout << "Not a Leap Year" << endl;

  return 0;
}

Output for different test:-

Enter Year: 2020
Leap Year

Enter Year: 2030
Not a Leap Year

Enter Year: 2050
Not a Leap Year

Enter Year: 1900
Not a Leap Year

Enter Year: 1700
Not a Leap Year

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Learn More C++ Programming Examples,

Leave a Comment

Your email address will not be published. Required fields are marked *