WIng prompts the user to enter the number of month and 2. Write a C++ program th
ID: 3914244 • Letter: W
Question
WIng prompts the user to enter the number of month and 2. Write a C++ program that do the followll urns the int variable to the caller. returns the int variable to the caller. false if it is not. a. Write a function getMonth, ear, prompts the user to enter the number of Year and ret the u b. Write a function get nCare a function isl cap Year, returns true if the Year is leap year and returns ction C. d. Wrte a function that returns number of days in that month. Pass month and e. Write a function NameofMonth, that returns name of the month. (E.G., January Year in the function. for 1, February for 2 Call the Functions in the main. Sample Output: Enter the Month: 2 Enter the Year: 2012 Year 2012 is a Leap Year. February 2012 has 29 Days.Explanation / Answer
Screenshot
------------------------------------------------------------------------------
Program
//Header File for I/O and string
#include<iostream>
#include<string>
using namespace std;
//function prototype
int getMonth();
int getYear();
bool isLeapYear(int yr);
int getNumOfDays(int mnth, int yr);
string getNameOfMonth(int mnth);
int main()
{
//call get month functon to prompt to get month from user
int mnth=getMonth();
//call get year functon to prompt to getyear from user
int yr=getYear();
cout << "*************************" << endl;
//Check leap year or not and display details
if (isLeapYear(yr) == 1) {
cout << "Year " << yr << " is a Leap Year." << endl;
cout << "**************************" << endl;
}
else {
cout << "Year " << yr << " is not a Leap Year." << endl;
cout << "******************************" << endl;
}
//call get days functon to find the number of days in the particular month of the year
int numDays = getNumOfDays(mnth, yr);
//call get month name functon to find the name of the month
string mnthName = getNameOfMonth(mnth);
//Display details of the enetred month and year
cout << mnthName << " " << yr << " has " << numDays << " Days." << endl;
return 0;
}
//Get month from user
int getMonth() {
int mon;
//Prompt user for month
cout << "Enter the Month: ";
cin >> mon;
//Check user entered month valididty
while (mon < 1 || mon>12) {
cout << "You entered wrong value please enter a number between 0-13!!!" << endl;
cout << "Enter the Month: ";
cin >> mon;
}
return mon;
}
//Getyear from user
int getYear() {
int yr;
//Prompt to enter year
cout << "Enter the Year: ";
cin >> yr;
//Check validity
while (yr<=0) {
cout << "You entered wrong value please enter a year greater than 0!!!" << endl;
cout << "Enter the Year: ";
cin >> yr;
}
return yr;
}
//Check leap year or not
bool isLeapYear(int yr) {
//If a year isleap year , when it is divisible by 4 and 400
if (yr % 4 == 0)
{
if (yr % 100 == 0)
{
if (yr % 400 == 0)
return true;
else
return false;
}
else
return true;
}
else
return false;
}
//Get number of days in particular month
int getNumOfDays(int mnth, int yr) {
//Using swith to get number of days of eaxh month
switch(mnth)
{
case 1:
return 31;
break;
case 2:
//If leap year then return 29 else return 28
if (isLeapYear(yr) == 1) {
return 29;
break;
}
else {
return 28;
break;
}
case 3:
return 31;
break;
case 4:
return 30;
break;
case 5:
return 31;
break;
case 6:
return 30;
break;
case 7:
return 31;
break;
case 8:
return 31;
break;
case 9:
return 30;
break;
case 10:
return 31;
break;
case 11:
return 30;
break;
case 12:
return 31;
break;
default:
break;
}
}
//Get name of the month
string getNameOfMonth(int mnth) {
//Using switch to get name of the month
switch (mnth) {
case 1:
return "January";
break;
case 2:
return "February";
break;
case 3:
return "March";
break;
case 4:
return "April";
break;
case 5:
return "May";
break;
case 6:
return "June";
break;
case 7:
return "July";
break;
case 8:
return "August";
break;
case 9:
return "September";
break;
case 10:
return "October";
break;
case 11:
return "november";
break;
case 12:
return "December";
break;
default:
break;
}
}
----------------------------------------------------------------------------
Output
Enter the Month: 2
Enter the Year: 2012
*************************
Year 2012 is a Leap Year.
**************************
February 2012 has 29 Days.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.