0% found this document useful (0 votes)
55 views

Experiments On Control Statements (Switch Statement)

The document describes a lab assignment on control statements in C++. The objectives are to learn about the switch statement and nested if statements. The tasks involve writing programs using switch statements to: 1) Create a basic calculator, 2) Determine days in a month, 3) Book seats in a stadium based on age, class and number of seats, 4) Check if a number is even or odd, and 5) Calculate reward points based on membership type and purchase amount. Sample solutions are provided for each task.

Uploaded by

Waseel sultan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Experiments On Control Statements (Switch Statement)

The document describes a lab assignment on control statements in C++. The objectives are to learn about the switch statement and nested if statements. The tasks involve writing programs using switch statements to: 1) Create a basic calculator, 2) Determine days in a month, 3) Book seats in a stadium based on age, class and number of seats, 4) Check if a number is even or odd, and 5) Calculate reward points based on membership type and purchase amount. Sample solutions are provided for each task.

Uploaded by

Waseel sultan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab4 - Week#5: Experiments on Control Statements (Switch

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.

Switch structure: alternate to if-else

o switch (integral) expression is


evaluated first
o Value of the expression determines
which corresponding action is taken
o Expression is sometimes called the
selector

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):

Sample run (3):

Task#2 Problem:

Write a C program to input month number and print total number of days in month using
switch statement.

Total days in a month is given by below table.

Month Total days


January, March, May, July, August, October, December 31 days
February 28/29 days
April, June, September, November 31 days

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:

1. Ask the user to enter the his/her id and age


2. If the age is 18 and above:
a. Ask the user to choose the class type: A, B or C
b. Different classes have different prices:
Class A : Price = 100$
Class B : Price = 75$
Class C: Price = 50$
c. Then, ask the user to enter number of seats want to book
d. Calculate the total cost ( No. of seats x Class price)
3. If the age is under 18, print message to the user: “Sorry, you are not allowed to book a seat
Output: age < 18

Output : age >18 , class = A , no. of seats = 5

Task#4 Problem:

Write a C program to input number from user and check whether the number is even or odd
(using switch statement).

Task#5: Post-Lab Exercise:

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)

Codes for tasks above.

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:

// booking seat(s) in a stadium


#include<iostream>
using namespace std;
int main()
{
int ID,NumberOfSeats;
float Age, price, Total;
char Class;
cout<<"Pleas enter your ID and Age....";
cin>>ID;
cin>>Age;
if (Age>=18)
{
cout<<"Pleas choose the class type....";
cin>>Class;
if (Class=='A')
price=100;
else if (Class=='B')
price=75;
else if (Class=='C')
price=50;
cout<<"Pleas enter the number of seats....";
cin>>NumberOfSeats;
Total= price* NumberOfSeats;
cout<<"The total cost is "<<Total;
}
else
cout<<"Sorry, you are not allowed to book a set";
}
return 0;
}

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;

cout<<"Enter (1, 2 or 3) for the membership type: \n";


cout<<"1. Standard \n";
cout<<"2. Plus \n";
cout<<"3. Premium \n";
cin>>membership_type;
cout<<"Enter the monthly purchase for the customer : ";
cin>>monthly_purchase;

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;
}

You might also like