by CodeChum Admin
Let’s try to find out whether a year has 365 or 366 days. To do this, we need to create a leap year detector!
Conditions for a leap year:
1.) year must be divisible by 4 and not divisible by 100
2.) If year is divisible by 100, it must be divisible by 400 for it to be a leap year.
Instructions:
Input
1. Year to be checked
Output
If a certain year is a leap year, print "<INSERT_YEAR_HERE> is a leap year"
Otherwise, print "<INSERT_YEAR_HERE> is not a leap year"
Enter·year:·2020
2020·is·a·leap·year
#include<iostream>
#include <string>
using namespace std;
bool isLeapYear(int year){
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0){
return true;
}
}
else{
return true;
}
}
return false;
}
int main()
{
int year;
cout<<"Enter year: ";
cin>>year;
if(isLeapYear(year)){
cout<<year<<" is a leap year\n\n";
}else{
cout<<year<<" is not a leap year\n\n";
}
system("pause");
return 0;
}
Comments
Leave a comment