C++ program to check given year is leap year or not
In a leap year, February has 29 days making it a total of 366 days in a year.
Following conditions are tested to determine a year is a leap year or not.
- Divisible by 4 and 400 - Leap Year
- Divisible by 100 - Not a Leap Year
- for rest - Not a Leap Year
To check the given year is a leap year or not, you need to examine the above condition and print the output accordingly.
Conditional Statements in C++
Code
#include <iostream>
using namespace std;
int main() {
int year;
cout<<"Enter a year: "; //requesting a year
cin>>year;
if(year%400==0){ //if divisible 400 it is a leap year
cout<<year<<" is a leap year";
}
else if(year%100==0){ //if divisible by 100 it is a not a leap year
cout<<year<<" is not a leap year";
}
else if(year%4==0){ //if divisible 4 it is a leap year
cout<<year<<" is a leap year";
}
else{ //if it does not satisfy any of the above conditions it is not a leap year
cout<<year<<" is not a leap year";
}
return 0;
}
Input
2018
Output
2018 is not a leap year
0 Comments