You are on page 1of 1

Sultan Qaboos University

College of Science
Department of Computer Science

COMP2002, Spring 2011

Week 5 Lab Exercises (‘if’ and ‘switch’ Statements)

Exercise 1: if/switch
Write a program planet.cpp that reads user weight (between 40 kg and 200 kg) and calculates
user weight on planets using the following formulas:

Weight on Mercury = Weight on Earth multiplied by 0.4155


Weight on Venus = Weight on Earth multiplied by 0.8975
Weight on Jupiter = Weight on Earth multiplied by 2.5374
Your program should display to the user a menu containing the THREE options where a
corresponding conversion formula will be processed according to the user’s selection. A suitable
error message should be displayed if an invalid choice is made.

---------------------------------------
1- Mercury
2-Venus
3-Jupiter
---------------------------------------
Your selection is >
Exercise 2: Nested if/switch
Rewrite the above program so that it reads a single line input containing THREE pieces of
information separated by a space; the 1st input is a numerical value representing the user’s weight,
the 2nd input is a letter indicating the planet which you want to convert the weight from, and the 3 rd
input is a letter indicating the planet which you want to covert the weight to. The weight can be any
value grater than ZERO while the 2nd and 3rd inputs must be one of the following: E=Earth,
M=Mercury, or V=Venus. For the weight conversion between different planets, use the following
table where ‘weight’ is the weight entered by the user:
From
Earth Mercury Venus
To
Earth Not allowed

Mercury weight × 0.4155 Not allowed

Venus weight × 0.8975 Not allowed

Submit your source file named lab5_xxxxx.cpp on Moodle before the end of the lab session
(replacing xxxxx with your student ID)

You might also like