ENG3202 Lab Report 4 - Switch
ENG3202 Lab Report 4 - Switch
ENG3202
Name: Fakhrul Hakim bin Anwar
No. Matrik: 228724
November 13
COMPUTER PROGRAMMING
LAB 4: SWITCH
1. Write a program using switch..case that will display a corresponding color code
according to a user input number. The corresponding color code is given in Table
4.1 below.
Table 4.1
0 = Black 3 = Orange 6 = Blue 9 = White
1 = Brown 4 = Yellow 7 = Violet
2 = Red 5 = Green 8 = Gray
Pseudocode :
1) Start The program.
2) Set a variable to be user input numerical value between 0 to 9
3) If the variable is equal to 0, Then print the text ‘Black’
4) If the variable is equal to 1, Then print the text ‘Brown’
5) If the variable is equal to 2, Then print the text ‘Red’
6) If the variable is equal to 3, Then print the text ‘Orange’
7) If the variable is equal to 4, Then print the text ‘Yellow’
8) If the variable is equal to 5, Then print the text ‘Green’
9) If the variable is equal to 6, Then print the text ‘Blue’
10) If the variable is equal to 7, Then print the text ‘Violet’
11) If the variable is equal to 8, Then print the text ‘Gray’
12) If the variable is equal to 9, Then print the text ‘White’
13) If the user input the variable not the number 0 to 9 , print " Please choose 0 to 9 only, program
will restart "
14) End the program
Flow Chart
Debug
Output
2. Rewrite the program in Question 1 using nested if..else statement
Pseudocode :
1) Start The program.
2) Set a variable to be user input numerical value between 0 to 9
3) If the variable is equal to 0, Then print the text ‘Black’
4) If the variable is equal to 1, Then print the text ‘Brown’
5) If the variable is equal to 2, Then print the text ‘Red’
6) If the variable is equal to 3, Then print the text ‘Orange’
7) If the variable is equal to 4, Then print the text ‘Yellow’
8) If the variable is equal to 5, Then print the text ‘Green’
9) If the variable is equal to 6, Then print the text ‘Blue’
10) If the variable is equal to 7, Then print the text ‘Violet’
11) If the variable is equal to 8, Then print the text ‘Gray’
12) If the variable is equal to 9, Then print the text ‘White’
13) If the user input the variable not the number 0 to 9 , print " Please choose 0 to 9 only, program
will restart "
14) End the program
Flow Chart
Debug
Output
Question 3
Table 4.2
Selangor/Faderal Territory of Kuala Lumpur & Price per m3
Putrajaya
Home (H)
0-20 m3 RM 0.57
More than 20 m3 RM 0.91
Minimum Charge RM 5.00
Industrial (I)
0-35 m3 RM 1.80
More than 35 m3 RM 1.92
Minimum Charge RM 30.00
Commercial (C)
0-35 m3 RM 2.00
More than 35 m3 RM 2.87
Minimum Charge RM 20.00
3. Write a program that will calculate and print out bills for Syarikat Air Selangor
Sdn. Bhd. (SYABAS). The water rates vary depending on whether the bill is for
home use, commercial use, or industrial use (refer to Table 4.2). A code of h means
home use, a code of c means commercial use and a code ofI means industrial use.
Any other code should be treated as an error. Thewater rates are computed as
given in the table.
Your program should prompt the user to enter an account number (type int), the
code (type char) and the volume (m3) of water used (type float). Your program
should echo the input data and print the amount due from the user. You must use
both if and switch statements.
Pseudocode:
Start the program
Prompt the user to enter 4 Variable
- Account number, A set as Float Value
- Code for Home(h) , or Industrial(I) or Comercial(c) and set as Character
- Volume of Water, V used set as float Value
if the user choose (h) , Then if (V<8.77193) then print ‘ Charges is = RM5.00 ‘
else if (V>8.77193 && V<=20) print “ Charges is : RM(calculate Value of charges = V*0.57) “
else if (V>20) then Print “ Charges is : RM(calculate Value of charges = 11.4 + (V-20)*0.91)”
if the user choose (I), Then if (V<16.66667) then print ‘Charges is = RM30.00’
else if (V>16.66667 && V<=35) print “ Charges is : RM(calculate Value of charges = V*1.80) “
else if (V>35) print “ Charges is : RM(calculate Value of charges = 63 + (V-35)*1.92) “
if the user choose (c), Then if (V<=10) then print ‘ Charges is = RM20.00 ‘
else if (V>20 && V<=35) Then print “ Charges is : RM(calculate Value of charges = V*2.00) “
else if (V>35) , Then print “ Charges is : RM(calculate Value of charges = 70 + (V-35)*2.87) “
Flow Chart
Debug
Output
Discussion and Analysis