Leap years contain one additional day (February 29) in order to keep the calenda
ID: 3791387 • Letter: L
Question
Leap years contain one additional day (February 29) in order to keep the calendar synchronized with the sun (because the earth revolves around the sun closer to once every 365.25 days). A year is a leap year if it is divisible by 4, unless it is a century (evenly divisible by 100) that is not divisible by 400. Note that 2015 is not a leap year, 1980 was a leap year, 2000 was a leap year, 1900 was not a leap year. Write a program that contains a user-defined function named is Leap Year, that takes a year value as an integer argument and returns true if the year is a leap year, false otherwise. Do not use the >> orExplanation / Answer
#include<iostream>
using namespace std;
bool isLeapYear(int year){
if ((year%4 == 0 && year%100 !=0)|| year%400 == 0)
{
return true;
}
else
{
return false;
}
}
int main()
{
int year;
char ch='y';
while(ch =='y'||ch=='Y'){
cout<<"Enter a year value: ";
cin >> year;
if(isLeapYear(year)){
cout<<year<<" is a leap year"<<endl;
}
else{
cout<<year<<" is not a leap year"<<endl;
}
cout<<"Continue? (y/n): ";
cin >> ch;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter a year value: 2015
2015 is not a leap year
Continue? (y/n): y
Enter a year value: 2000
2000 is a leap year
Continue? (y/n): y
Enter a year value: 1900
1900 is not a leap year
Continue? (y/n): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.