Experiments On Control Statements (Switch Statement)
Experiments On Control Statements (Switch Statement)
statement)
Objective(s)
1. Learn about Switch statement in C++.
2. Learn about nested if statement.
Tool(s)/Software
DevC++.
Description
• Using switch statement in C++ programs
• Using if statement (if, if..else and Nested if) in C++ programs.
Tasks/Assignments(s)
Task#1 Problem:
Write a C++ program (using switch statement) for a simple calculator. Your program should
read from the user which mathematic operation s/he would like to perform (+, -, *, /) and should
read two numbers to perform the operation on.
There are three possible sources of user error you should consider:
1. The user could enter an invalid operation (something other than +, -, * or /).
2. The user could enter / for operation and 0 for number2 (i.e., a divide-by-zero error).
Sample run (1): Sample run (2):
Task#2 Problem:
Write a C program to input month number and print total number of days in month using
switch statement.
Task#3 Problem:
Write a program that allow the user to book seat(s) in a stadium and calculate the total cost
according to the following:
Task#4 Problem:
Write a C program to input number from user and check whether the number is even or odd
(using switch statement).
Create a program that displays the number of reward points a customer earns each month.
The reward points are based on the customer’s membership type and total monthly purchase
amount, as shown in table below:
Deliverables(s)
Task#1 Solution:
// Calculator
#include <iostream>
using namespace std;
int main()
{
int a,b;
char op;
cout<<"enter a value :" << endl;
cin >> a;
cout<<"enter b value :" <<endl;
cin >> b;
cout<<"enter operation (+ , - , * , / ):";
cin>> op;
switch (op)
{
case '+': cout<<"The result = "<<a+b; break;
case '-': cout<<"The result = "<<a-b; break;
case '*': cout<<"The result = "<<a*b; break;
case '/': cout<<"The result = "<<a/b; break;
default: cout<<"entered wrong input";
}
return 0;
}
Task#2 Solution:
int main()
{
int month;
// Input month number from user
cout<< "Enter month number(1-12): ";
cin>> month;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
cout<< "31 days";
break;
case 2:
cout<< "28/29 days";
break;
case 4:
case 6:
case 9:
case 11:
cout<<"30 days";
break;
default:
cout<<"Invalid input! Please enter month number between 1-12";
}
return 0;
}
Task#3 Solution:
Task#4 Solution:
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"enter a number :" << endl;
cin >> a;
if (a%2 == 0)
cout<< a<<”is even”;
else
cout<<a <<”is odd”;
return 0;
}
Task#5 Solution:
int main()
{
int membership_type;
double monthly_purchase;
int reward_points;
switch (membership_type)
{
case 1:
{
if (monthly_purchase < 75 )
reward_points = 0.05 * monthly_purchase;
else if (monthly_purchase < 150)
reward_points = 0.075 * monthly_purchase;
else
reward_points = 0.1 * monthly_purchase;
}
cout<<"Rewards points : "<<reward_points<<endl;
break;
case 2:
{
if (monthly_purchase < 150)
reward_points = 0.06 * monthly_purchase;
else
reward_points = 0.13 * monthly_purchase;
cout<<"Rewards points : "<<reward_points<<endl;
}
break;
case 3:
{
if (monthly_purchase > 200)
reward_points = 0.04 * monthly_purchase;
else
reward_points = 0.15 * monthly_purchase;
cout<<"Rewards points : "<<reward_points<<endl;
}
break;
default:
cout<< "You entered invalid number for the membership type.";
}
return 0;
}