Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using c++, Write a leapYear() function that accepts the year as an integer and r

ID: 3763284 • Letter: U

Question

using c++, Write a leapYear() function that accepts the year as an integer and returns true if the year is a leap year and false if it is not a leap year. Usually, years that are divisible by 4 are leap years, for example 1996. However, years that are divisible by 100 (for example, 1900) are not leap years, but years that are divisible by 400 are leap years (for example, 2000).

Extra Credit: As a comment at the very bottom of your program, explain why the year 1582 is significant in the leap year world.

Explanation / Answer

Solution:

#include <iostream>
using namespace std;
bool leapYear(int);

int main() {

int year;
cout << "Enter a year: ";
cin >> year;
bool res = leapYear(year);
if (res == true) {
cout << year << " is a leap year.Result is TRUE";
}else {
cout << year << " is not a leap year.Result is FALSE";
}

return 0;
}

bool leapYear(int year){
bool result;
if (year%4 == 0) {
if (year%100 == 0) {
if (year%400 == 0) {
result = true;
}
else {
result = false;
}
}
else {
result = true;
}
}
else {
result = false;
}
return result;
}

Output:

Enter a year: 2016

2016 is a leap year.Result is TRUE

___________________________________________________________________________________

The Gregorian calendar was introduced in 1582.After 1582, years divisible by 100 are not leap years unless they are also divisible by 400. Thus, 1900 is not a leap year, but 2000 is. The Gregorian Calendar is a vast improvement over the Julian Calendar, which was used previously, but still not perfect.

For ordinary purposes, the dates of events occurring prior to 15 October 1582 are generally shown as they appeared in the Julian calendar, with the year starting on 1 January, and no conversion to their Gregorian equivalents.