You are on page 1of 2

LAB MANUAL # 4

Course: Computer Programming (CP-107)


Session: 21CP

More Branching statements (switch, goto, enum)

This lab has been designed to practice some more branching statements e.g., switch
statement, goto statement, conditional operator and enum data type. Clear concept about
this programming construct has already been provided in the class. Supporting material can
be found in section 3.1 and 3.2 of Problem solving with C++. Lab handouts are also provided
along with the lab manual as reference material.
Objectives:
In this lab, you will practice:
• Using switch statement as an alternative to if-else statement.
• Using goto statement.
• Using conditional operator.
• Creating and using Enumeration data type.

Lab Tasks:
Question # 1:
Write a program in C++ to input a single digit from 0 to 9 and print the input value in words.
For example, if the input value is 0, then print “zero”. Use switch statement.
Question # 2:
Write a program using switch statement to check whether an integer entered by the user is
even, odd or an invalid number.
Question # 3:
Write a program that presents the user w/ a choice of your 5 favourite beverages (Coke,
Water, Sprite ,..., Whatever). Then allow the user to choose a beverage by entering a number
1-5. Output which beverage they chose. If the user enters a choice other than 1-5 then it will
output "Error. Choice was not valid”, here is your money back."
Question # 4:
Write a program to build a simple calculator using switch statement. Calculator should be able
to perform following five operations i.e., (+,-,*,/, %). Output should be like this:
Sample Output:
Enter an operator (+, -, *, /, %): +
Enter two numbers:
4
LAB MANUAL # 4
Course: Computer Programming (CP-107)
Session: 21CP

6
4+5=9
Question # 5:
Write a program using switch statement that displays the following lunch menu to the
customer.
1-Burger
2-French Fries
3-Pizza
4-Rice
5-Sandwiches
The program ask customer to enter food type and quantity. If then displays the total
chargers for the order according to following price list:
Burger=Rs. 150
French Fries=Rs. 50
Pizza=Rs. 500
Rice=Rs. 200
Sandwiches=Rs. 100
Use enumeration to define food menu.
Question # 6:
Write a program using switch statement that asks the user to enter grade from (A to F)
and prints remarks against that grade. E.g., In case of grade A program should print
Excellent. Program should print in case of both lower case or upper-case grade letters.
Question # 7:
Write a program to print first ten natural numbers. Use “goto” statements
Question # 8:
Write a program to check whether year entered by the user is leap year or normal year using
conditional operator.

***************

You might also like