C++ Program to Check Leap Year or not

Program to check whether a year is a leap year or not

Here we will discuss how to check whether a year is a leap year or not using the C++ programming language.

In a  year there are 365 days, but once every fourth year there are 366 days that year is called a leap year.

Leap year comes in every four year, how to know that the year is a leap year or not, there are two ways to know,

  1. First is, All those years which are divided by 4 are leap years, except century years.
  2. Second is,if a century year is divisible by 400 then the year is a leap year.
Program to check whether a Year is a Leap Year or not

Working

Let’s see how the code works

  • User gives an input.
  • The input is stored in an int type variable say year.
  • year is checked for being a leap year or not with the following condition 

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

  • If the above condition is true then input is a leap year otherwise input is not a leap year.

C++ Code

//C++ Program
//Leap year or not
#include<iostream>
using namespace std;
//main program
int main()
{
//initialising variables
int year;
cout<<"Enter year to check: ";
//user input
cin>>year;
//checking for leap year
if( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400==0) )
{
//input is a leap year
cout<<year<<" is a leap year";
}
else
{
//input is not a leap year
cout<<year<< " is not a leap year";
}
return 0;
}

Output

Enter year to check: 2019
2019 is not a leap year

Please Login/Signup to comment