C++ Program to Find Year is leap Year or not

In this program, you will ask the user to enter a year and check whether the year is a leap year or not.

For checking leap year. the following conditions should satisfy.

  1. when the year is divisible by 4 but not by 100 then leap year.
  2. when a year is divisible by 400 then the year will be a leap year.

Check leap year using if else


#include <iostream>
using namespace std;

int main() {

  int year;

  cout << "Enter a year to check leap year:";
  cin >> 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;
}

output
Enter a year to check leap year:2000
2000 is a leap year
Enter a year to check leap year:2010
2010 is not a leap year

Check leap year using instead if else


#include <iostream>
using namespace std;

int main() {

  int year;
  cout << "Enter a year to check leap 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;
}
output
Enter a year to check leap year: 2020
2020 is a leap year.

Check leap year program using function


#include <iostream>
using namespace std;

/* function for checking leap year. it will return true or false */
bool leapYear(int year){
   bool isLeapYear = false;
   if (year % 4 == 0) {
      if (year % 100 == 0) {
         if (year % 400 == 0) {
            isLeapYear = true;
         }
      } 
      else isLeapYear = true;
   }
   return isLeapYear;
}

int main(){
   int year;
   cout<<"Enter a year to check leap year: ";
   cin>>year;
   
   //Calling leapYear function
   bool flag = leapYear(year);
   
   if(flag == true)  
      cout<<year<<" is a leap Year"; 
   else 
      cout<<year<<" is not a leap Year";
   return 0;
}
output
Enter a year to check leap year: 2024
2024 is a leap Year

Time Complexity: O(1)