Class Work
Class Work
tables
Practice questions Exercise
1 Write an algorithm and python code to print the value of sum1 if it is less than sum2.
Algorithm python
2 Write an algorithm and python code to print the smaller of the contents of two variables
Algorithm python
3. Write an algorithm and python code to read values into variables A & B. The algorithm should store the
smaller in A and the larger in B and then print A and B.
Algorithm python
4. Write an algorithm to read values in variables A & B and divide the larger number by the smaller number.
Algorithm python
5. Write an algorithm to read values into variables A, B, and C and print the smallest of the numbers.
6 Write an algorithm and python code to prompt the user for a number. If the number is greater than 45
the pseudocode should prompt the user for three additional numbers and find their sum otherwise it
should terminate.
7 Write an algorithm to read values into four variables A, B, C and D and print the smallest and the
largest of the four variables.
Algorithm python
# Step 1: Start
1. Start
2. Read “value into variable”, A
3. Read “value into variable”, B
4. Read “value into variable”, C
5. If A < 0, then
6. Set sum = B + C
7. Print sum
Else
8. Set average = (A + B + C) / 3
9. Print average
10. Stop
Main Menu
Select option:
9 Write an algorithm to read a value into a variable of your choice. If the value entered is 1 the
pseudocode should print your name, if the value entered is 2 the pseudocode should print your school name,
if the value entered is 3 the pseudocode, it should print your favorite color, otherwise the program should
terminate.
Algorithm python
10. Write a program that converts the input Celsius degree into its equivalent Fahrenheit degree. Use the
formula: F = (9/5) *C+32.
Algorithm python
This code follows each step of the algorithm. The variables F and C are
initialized to 0 (step 2). The user is prompted to enter the temperature in
Celsius (step 3), and it is stored in the variable C. The code then converts
Celsius to Fahrenheit using the formula (1.8 * C) + 32 (step 4). Finally, the
converted temperature is printed to the console (step 5).
11. Write a program that converts the input dollar to its peso exchange rate equivalent. Assume that the
present exchange rate is 51.50 pesos against the dollar. Then display the peso equivalent exchange rate.
Algorithm python
12. Write a program that converts an input inch(es) into its equivalent centimeters. Take note that one inch is
equivalent to 2.54cms
Algorithm python
This code follows each step of the algorithm. The user is prompted to enter
the length in inches (step 2), and it is stored in the variable inch. The code
then calculates the equivalent length in centimeters using the formula 2.54 *
inch (step 3). Finally, the converted length is printed to the console (step 4).
Create an app to find a solution to the following problem.
MENU
1. Converts Celsius degree into its equivalent Fahrenheit degree
2. Converts BZ dollar to Pesos
3. Converts an inch(es) into its equivalent centimeters
4. (0) to quit
Option:
13. Write a program that exchanges the value of two variables: x and y. The output must be: the value of
variable y will become the value of variable x, and vice versa.
Algorithm python
In this code, the user is prompted to enter the values of x and y (step 2),
which are then stored in the respective variables. The values of x and y are
swapped using a third variable z (step 3). Finally, the swapped values
of x and y are printed to the console (step 4).
14. Design a program to find the circumference of a circle. Use the formula: C=2πr, where π is approximately
equivalent 3.1416.
Algorithm python
In this code, we import the math module to access the value of pi. The user is
prompted to enter the radius (step 2), and it is stored in the variable r. The
code then calculates the circumference using the formula 2 * math.pi *
r (step 3). Finally, the calculated circumference is printed to the console (step
4).
14. Write a program that takes as input the purchase price of an item (P), its expected number of years of
service (Y) and its expected salvage value (S). Then outputs the yearly depreciation for the item (D). Use the
formula: D = (P - S) Y.
Algorithm python
In this code, the user is prompted to enter the values of P, S, and Y (steps 2, 3,
and 4). These values are stored in the respective variables. The code then
calculates D using the formula (P - S) * Y (step 5). Finally, the calculated value
of D is printed to the console (step 6).
15. Create a program to compute the volume of a sphere. Use the formula: V = (4/3) *pi*r3 where pi is equal
to 3.1416 approximately. The r is the radius of sphere. Display the result.
Algorithm python
PROBLEM SOLVING
SUM = 0
N = 20
WHILE N < 30 DO
SUM = SUM + N
N=N+3
Print n
ENDWHILE
Read X
For M = 1 to X do
Y=X–M
Z=5*Y–M
END
Print Z
Copy the following trace table. Complete the trace table, given that the number 4 is the input
value for X.
X=4
X M Y Z M=1
Read X
4 1 Y=0
For M = 1 to X do
4 8 Z=0
4
Y=X–M while M <= X:
4
Z=5*Y–M Y=X-M
END Z=5*Y-M
M += 1
Print Z
print(Z)
COUNT = 1
while COUNT < 26:
X = X * 2
print(COUNT, X)
X=2
COUNT = COUNT + 5
WHILE COUNT < 26 DO
X=X*2
Print COUNT, X
COUNT = COUNT + 5
ENDWHILE
X=5
X K SUM
X = 5
K = 10
K = 10
SUM = 45 5 10
SUM = 45
DIFFERENCE = A – B
if A <= B:
DIFFERENCE = A - B
else:
ELSE DIFFERENCE = B - A
What does the algorithm above print if the input values are as stated in the table above?
CXC 1999 Quest 9 a
A B DIFFERENCE
20 30
100 100
50 10
Trace Table
dry run
5
A=A+B
B=B+A
SUM = SUM + B
ENDWHILE
Print SUM
STOP
b. State what the algorithm prints. CXC 2000 Quest. 8 b & c
2. Read Num
Num J X
J=1
X = (J * 2) – 2
Print J
J=J+2
ENDWHILE
a. Given that 10 is the input value, create a trace table for the algorithm above using Num, J and X as table headings.
b. Given that 10 is the input value, determine the output of the algorithm.
CXC 2002 Jan.
3 Write an algorithm or program to read in three numbers and find the average of the numbers.
CXC 2002 June Quest 11
4. a. Copy and complete the following trace table, for the algorithm.
X=1
X Y Z
Y=2
1 2 3
Z=3
4
Z=Z+X
X=Z–X
Y=Z+Y
Z=Y–Z
Y=Y–X–Z
Print X, Y, Z
END
X=1 X Y Z
Y=2 1 2 3
Z=3
X=X+Y
Y=X*Y
Z=X+Y
X=X+Y
Y=X*Y
IF Z < X, THEN X = Z
IF Z > =X, THEN Y = X
Print X, Y, Z
END
X=3 X Y Z
Y=2 3 2 1
Z=1
DO WHILE Z > 0 THEN
X=X*Y
Y=Y+Z
Z=Z–1
LOOP
Print X, Y, Z
END