Using the switch statement
a. Write a C++ program that asks the user to input two numbers and an operator (+, -,
*, or /), and then performs the operation (addition, subtraction, multiplication,
or division) on the two entered numbers according to the operator input by the
user. If the entered operator is not from these four operators, the program
should display an error message saying that the entered operator is invalid.
b. Write a program that should ask the user to input a month number (from 1 to
12) and then display the respective month name in words (like January for 1,
February for 2 and so on). If the user inputs value other than numbers from 1 to
12, the program should display a message saying that it is an invalid month
number.
c. Write a program that asks the user to input a month (from 1 to 12) and then
display which season currently it is. The season should be displayed as per the
following rules
i. If the month number is 12 or 1 or 2, then it is the Winter season
ii. If the month number is 3 or 4, then it is the Spring season
iii. If the month number is 5 or 6 or 7 or 8 or 9, then it is the Summer season
iv. If the month number is 10 or 11, then it is the Autumn season
d. Write a program that should ask the user to input an alphabet character (small
or capital) and tell if it is a vowel or a consonant.
e. Write a program that should ask the user to input an integer number and print
whether it is even or odd.
========================================================================
a. Write a program that asks the user to input two numbers and an operator (+, -, *, or /), and then performs the operation (addition, subtraction, multiplication, or division) on the two entered numbers according to the operator input by the user. If the entered operator is not from these four operators, the program should display an error message saying that the entered operator is invalid.
#include <iostream>
using namespace std;
int main()
{
int number1, number2;
char op;
cout<<"Please enter first number : ";
cin>>number1;
cout<<"Please enter second number : ";
cin>>number2;
cout<<"Please enter an operator +, -, *, /, % : ";
cin>>op;
switch(op)
{
case '+':
cout<<"Addition of your entered numbers is "<<number1+number2;
break;
case '-':
cout<<"Subtraction of your entered numbers is "<<number1-number2;
break;
case '*':
cout<<"Multiplication of your entered numbers is "<<number1*number2;
break;
case '/':
cout<<"Division of your entered numbers is "<<float(number1/number2);
break;
case '%':
cout<<"Remainder of your entered numbers is "<<number1%number2;
break;
default:
cout<<"Please enter a valid input";
}
return 0;
}
b. Write a program that should ask the user to input a month number (from 1 to 12) and then display the respective month name in words (like January for 1, February for 2 and so on). If the user inputs value other than numbers from 1 to 12, the program should display a message saying that it is an invalid month number.
#include <iostream>
using namespace std;
int main()
{
int number;
cout<<"Please Enter a number 1-12 : ";
cin>>number;
switch (number)
{
case 1:
cout<<"January";
break;
case 2:
cout<<"February";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"Octomber";
break;
case 11:
cout<<"November";
break;
case 12:
cout<<"December";
break;
default:
cout<<"Invalid";
}
return 0;
}
c. Write a program that asks the user to input a month (from 1 to 12) and then display which season currently it is. The season should be displayed as per the following rules i. If the month number is 12 or 1 or 2, then it is the Winter season ii. If the month number is 3 or 4, then it is the Spring season iii. If the month number is 5 or 6 or 7 or 8 or 9, then it is the Summer season iv. If the month number is 10 or 11, then it is the Autumn season
#include <iostream>
using namespace std;
int main()
{
int number;
cout<<"Please Enter a number to check whether 1-12 : ";
cin>>number;
// if (number == 1 || number == 2 || number == 12)
// {
// cout<<"Winter Season";
// }
// else if (number == 3 || number == 4)
// {
// cout<<"Spring Season";
// }
// else if (number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
// {
// cout<<"Summer Season";
// }
// else if (number == 10 || number == 11)
// {
// cout<<"Autumn Season";
// }
// else
// {
// cout<<"Invalid";
// }
switch(number)
{
case 1:
cout<<"Winter";
break;
case 2:
cout<<"Winter";
break;
case 12:
cout<<"Winter";
break;
case 3:
cout<<"Spring";
break;
case 4:
cout<<"Spring";
break;
case 5:
cout<<"Summer";
break;
case 6:
cout<<"Summer";
break;
case 7:
cout<<"Summer";
break;
case 8:
cout<<"Summer";
break;
case 9:
cout<<"Summer";
break;
case 10:
cout<<"Autumn";
break;
case 11:
cout<<"Autumn";
break;
default:
cout<<"Please enter a valid input";
}
return 0;
}
d. Write a program that should ask the user to input an alphabet character (small or capital) and tell if it is a vowel or a consonant.
#include <iostream>
using namespace std;
int main()
{
char alphabet;
cout << "Please enter a character : ";
cin >> alphabet;
// if (alphabet != 'a' && alphabet != 'A' && alphabet != 'e' && alphabet != 'E' && alphabet != 'i'
// && alphabet != 'I' && alphabet != 'o' && alphabet != 'O' && alphabet != 'u' && alphabet != 'U')
// {
// cout << "You have entered a consonant character";
// }
// else
// {
// cout << "You have entered a vowel character";
// }
switch (alphabet)
{
case 'a':
cout<<"Vowel";
break;
case 'e':
cout<<"Vowel";
break;
case 'i':
cout<<"Vowel";
break;
case 'o':
cout<<"Vowel";
break;
case 'u':
cout<<"Vowel";
break;
case 'A':
cout<<"Vowel";
break;
case 'E':
cout<<"Vowel";
break;
case 'I':
cout<<"Vowel";
break;
case 'O':
cout<<"Vowel";
break;
case 'U':
cout<<"Vowel";
break;
default:
cout<<"Consonant";
}
return 0;
}
e. Write a program that should ask the user to input an integer number and print whether it is even or odd.
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please enter a number : ";
cin >> number;
int answer = number % 2;
switch (answer)
{
case 0:
cout<<"Even";
break;
default:
cout<<"odd";
}
return 0;
}