In: Computer Science
C++ questions, please make sure to dividet he program into functions which perform each major task,
A leap year is defined as any calendar year which meets the following criteria:
If the year is not divisible by 4, it is a common year
If the year is not divisible by 100, it is a leap year
If the year is not divisible by 400, it is a common year
Otherwise it is a leap year
For your program you will take in three integers from the user:
A four digit year
The day of the month (between 1 and 31)
The month (between 1 and 12, 1 representing January and 12 representing December)
Your program will output whether or not the date entered is a valid calendar date. Be sure to divide your program into functions which perform each major task.
Hint: There are 30 days in September, April, June and November. If the year is a leap year, there are 29 days in February. If the year is a common year there are 28 days in February. All other months have 31 days. Some examples to help you verify leap year calculation:
Condition |
Result |
Examples |
Not divisible by 4 |
Not a leap year |
2009, 2010, 2011 |
Divisible by 4 |
Leap year |
2008, 2012, 2016 |
Divisible by 100 |
Not a leap year |
1800, 1900, 2100 |
Divisible by 400 |
Leap year |
2000, 2400 |
#include <iostream>
using namespace std;
void leapyear(int year) {
//Giving conditions for each year dividing by 4,100 and 400
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
}
int main()
{
int year;
//Taking user input
cout << "Enter a year: ";
cin >> year;
//Calling leap year function
leapyear(year);
return 0;
}
Sample input and output:
Enter a year: 2010
2010 is not a leap year.
Enter a year: 2016
2016 is a leap year.
#include <iostream>
using namespace std;
void leapyear(int year) {
//Giving conditions for each year dividing by 4,100 and 400
if (year % 4 == 0)
{
cout << year << " is divided by 4.\n";
if (year % 100 == 0)
{
cout << year << " is divided by 100.\n";
if (year % 400 == 0) {
cout << year << " is divided by 400.\n";
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else {
cout << "Not divisible by 4.\n";
cout << year << " is not a leap year.";
}
}
int main()
{
int year;
//Taking user input
cout << "Enter a year: ";
cin >> year;
//Calling leap year function
leapyear(year);
return 0;
}
Sample input and output:
Enter a year: 2009
Not divisible by 4.
2009 is not a leap year.
Enter a year: 1800
1800 is divided by 4.
1800 is divided by 100.
1800 is not a leap year.