EKT120 Lab4 Extra
EKT120 Lab4 Extra
LAB 4(EXTRA)
LAB EXERCISE
1. By using the aid of flowchart(s), explain the difference between if selection and if…
else selection.
Marks Grade
80-100 A
70-79 B
60-69 C
40-59 D
20-39 E
0-19 F
3. The following questions are based on the program segment in Figure 3(a) and Figure 3(b):
4. Identify whether if-else statement in Question (2) can or cannot be converted into switch
statements. Justify your answer.
5. What are differences between switch statement and if-else if statement control
structures? By using the aid of flowchart(s), explain the differences.
6. Write a program segment using switch selection for the following statements.
If the grade is A, print “Your CGPA is 4.0”.
If the grade is B, print “Your CGPA is 3.0”.
If the grade is C, print “Your CGPA is 2.0”.
If the grade is D, print “Your CGPA is 1.0”.
#include<stdio.h>
int main()
{
int num=7;
if(num>0)
{
printf(“%d is a positive number\n”,num);
if(num %2 ==0)
printf(“%d is an even number\n”,num);
else
printf(“%d is an odd number\n”,num);
}
else
printf(“%d is a negative number\n”,num);
return 0;
}
EKT 120 - Computer Programming
LAB 4(EXTRA)
1. Write a program that calculates the user’s body mass index (BMI) and categorizes it as
underweight, normal, overweight, or obese, based on the following table:
To calculate BMI based on weight in kilograms (kg) and height in meters (m), use this
formula:
weight
BMI
height height
2. Write a program to input a number x from the keyboard. If the number is larger than 0,
find its square root. Otherwise, calculate x* x. Write the program by using selection
statement. A sample of the output showed as below.
3. A company sells five different products with prices shown in the following table
Write a program using switch-case statement that reads the product number and
number of units sold and display the amount to be paid.
4. Create a C program to determine the status and grade of Computer Programming subject based on
midterm examination and final examination. The midterm examination contributes 30% and final
examination contributes 70% of final marks. The program should read input of both examination
marks (full marks is 100 for both examination), calculates and display the final marks. Finally, the
program will display the status and grade as shown in Table 4.1 based on calculated final marks.
ADDITIONAL TASKS
1. Develop a program for current electrical billing system based on the given requirements
using the if..else statements shown in Table 1.2.
Price per k/watt is: 0.50 per unit for 300 units and 0.65 per unit for 135
units
Total amount of bill per k/watt: RM237.75
The speed and the most appropriate transportation choices is shown in Table 2.2.
ax2+ bx + c = 0
The input data is to consist of the values of a, b and c and is to come from the keyboard.
The output is to consist of the values of x and display to the user. However, your computer
must consider all the possibilities, if b2 – 4ac is positive, then equation (1.1 and 1.2) can be
used directly to find the solutions x1 and x2. However, if the b2 – 4ac is negative, the
equation become as in 1.3 and 1.4. Below are the equations.
b b 2 4ac
x1 (3.1)
2a
b b 2 4ac
x2 (3.2)
2a
b (b 2 4ac)
x1 (3.3)
2a 2a
b (b 2 4ac)
x2 (3.4)
2a 2a
4. Write a C program to measure perimeter and area of circle and rectangle using switch case
statements. The program should read type of shape (‘C’ for circle or ‘R’ for rectangle) from users.
If users enter ‘C’, prompt users to enter the radius of circle. Otherwise, if users enter ‘R’, prompt
users to enter the width and height of the rectangle. If users enter other than ‘C’ or ‘R’, display
invalid message. Then, determine the perimeter and area of the corresponding shape. The formula
of both shape is shown in Table 4 (Given π=3.142).
Perimeter : 18.85 cm
Area : 28.28 cm2
Perimeter : 22.00 cm
Area : 28.00 cm2