0% found this document useful (0 votes)
60 views1 page

Week 5 Lab Exercises PDF

This document provides instructions for two programming exercises involving if and switch statements. The first exercise asks students to write a program that reads a user's weight and displays their weight on Mercury, Venus, or Jupiter based on the planet they select from a menu. The second exercise modifies the program to take a single input of weight, starting planet, and ending planet to calculate the weight conversion between planets based on given formulas. Students are to submit their source code before the end of the lab session.

Uploaded by

Abdul Aziz Turdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views1 page

Week 5 Lab Exercises PDF

This document provides instructions for two programming exercises involving if and switch statements. The first exercise asks students to write a program that reads a user's weight and displays their weight on Mercury, Venus, or Jupiter based on the planet they select from a menu. The second exercise modifies the program to take a single input of weight, starting planet, and ending planet to calculate the weight conversion between planets based on given formulas. Students are to submit their source code before the end of the lab session.

Uploaded by

Abdul Aziz Turdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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