In: Computer Science
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till they enter -1. It's a c++ program.
C++ code:
#include <iostream>
using namespace std;
int main(){
//initializing month
int month;
//asking for month
cout<<"Enter month: ";
//accepting month
cin>>month;
//looping till month is -1
while(month!=-1){
//switching for
month
switch(month){
//checking if month number is 1,3,5,7,8,10,12
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
//printing Number of days is 31
cout<<"Number of days is 31"<<endl;
//exiting switch case
break;
case 2:
//printing Number of days is 28
cout<<"Number of days is 28"<<endl;
//exiting switch case
break;
case 4:
case 6:
case 9:
case 11:
//printing Number of days is 30
cout<<"Number of days is 30"<<endl;
//exiting switch case
break;
default:
//printing invalid Number of days
cout<<"Invalid number of days"<<endl;
}
//asking for month
cout<<"Enter
month: ";
//accepting month
cin>>month;
}
return 0;
}
Screenshot:
Input and Output: