1t Task: Create a program that uses a switch statement and case statements to ca
ID: 3585331 • Letter: 1
Question
1t Task: Create a program that uses a switch statement and case statements to calculate the number of days in any given month based upon a user's input. February is a unique month. Prompt the user again to input the year if the user wants the number of days for February. Hint**. You will need to solve for leap year. Have the user input information. Make sure the program can deal with any integer input. If a number entered does not correspond to a month of the year print "Invalid Month." Attach a snipping photo of source code and output Sample: Please enter Month 1-12 31 days in January Please enter Month 1 -12 You selected February, Please enter year: 2017 28 DaysExplanation / Answer
#include <iostream>
using namespace std;
int main() {
int month, year;
cout<<"Enter month between 1 - 12: ";
cin>>month;
switch(month) {
case 1 :
cout<<"31 days in January";
break;
case 2 :
cout<<"You selected Feburary, please enter Year: ";
cin>>year;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << "29 Days";
else
cout <<"28 Days";
}
else
cout<<"29 Days";
}
else
cout << "28 Days";
break;
case 3 :
cout<<"31 days in March";
break;
case 4 :
cout<<"30 days in April";
break;
case 5 :
cout<<"31 days in May";
break;
case 6 :
cout<<"30 days in June";
break;
case 7 :
cout<<"31 days in July";
break;
case 8 :
cout<<"31 days in August";
break;
case 9 :
cout<<"30 days in September";
break;
case 10 :
cout<<"31 days in October";
break;
case 11 :
cout<<"30 days in November";
break;
case 12 :
cout<<"31 days in December";
break;
default :
cout<< "Invalid Month";
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.