C++ program to check leap year


In this example, you will learn about C++ program to check leap year in two different ways.

How is leap year calculated?

A year that is exactly divisible by four is a leap year, except for years that are divisible by 100, but these century years are leap years if they are exactly divisible by 400.

Example: C++ program to check leap year.

//Easy C++ program to check for leap year
#include <iostream>
using namespace std;

int main ()
{
   int year;

   cout << "Enter a year you want to check: ";
   cin >> year;

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

   return 0;
}

Output

C++ program to check leap year

Example: C++ program for leap year in single line

//C++ program for leap year in single line
#include <iostream>
using namespace std;

int main()
{
   int year;

   cout << "Enter a year you want to check: ";
   cin >> year;

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

   return 0;
}

Output

Enter a year you want to check: 1990
1990 is not a leap year.

Enter a year you want to check: 2000
2000 is a leap year.