Question : Write a C++ program to Check Whether Year is Leap Year or Not
?
Explain: C++ Question about check input year from user then program tells us which given year is Leap or Not. We will used simple If-else statement for resolving solution.
What is Leap Year?
After Every four years one day increase in Feb month (29 days ) then year will be contain 366 days , called leap year
We can calculate Leap Year with following Steps .
- The year can be evenly divided by 4;
- If the year can be evenly divided by 100, it is NOT a leap year, unless;
- The year is also evenly divisible by 400. Then it is a leap year.
Solution:
/************************************************************
C++ Program for check Leap Year using if else
*************************************************************
***************** By cpplanguage.com *********************/
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter a Year: ";
// Getting Year as input
cin >> year;
//check year divided by 4 and not divided by 100, then Leap Year
if((year%4==0) && (year%100!=0))
{
cout<<year<<" is a Leap Year"<<endl;
}
// if year divided 100 and reminder 0 , Not Leap Year
else if(year%100==0)
{
cout<<year<<" is not a Leap Year"<<endl;
}
// if year divided 400 and reminder 0 , Leap Year
else if(year%400==0)
{
cout<<year<<" is a Leap Year"<<endl;
}
// Other wise , Not Leap Year
else
{
cout<<year<<" is not a Leap Year"<<endl;
}
return 0;
}
Output: