EXAMPLE 1:
PSEUDOCODE TO ADD 10 NUMBERS TOGETHER USING FOR LOOP or WHILE LOOP and
output the Sum and average.
COUNT-CONTROLLED LOOP
total 0
FOR count 1 to 10
OUTPUT “Enter a value”
INPUT value
total total + value
NEXT count
OUTPUT total
Average total/10
OUTPUT average
CONDITION-CONTROLLED LOOP (Ex. 1)
total 0
count 0
INPUT number
WHILE count <number DO
OUTPUT “Enter a value:”
INPUT value
total total + value
count count+1
ENDWHILE
OUTPUT total
Averagetotal/10
OUTPUT average
CONDITION-CONTROLLED LOOP (Ex. 2)
total 0
count 0
INPUT number
REPEAT
OUTPUT “Enter a value:”
INPUT value
total total + value
count count+1
UNTIL count = number
OUTPUT total
Average total/10
OUTPUT average
CONDITION-CONTROLLED LOOP (Ex. 3)
INPUT password
WHILE password <> ”myschool123” DO
OUTPUT “Enter your password”
INPUT password
ENDWHILE
OUTPUT “Bravo! Correct password.”
Example 2:
PRINT (“Enter number 1”)
INPUT number1
PRINT (“Enter number 2”)
INPUT number2
Sum number1 + number2
product number1 * number2
PRINT (“the sum is” + sum)
PRINT (“the product is”+ product)
Example 3:
ACTIVITY 4a (Hodder Textbook)
Number 1:
OUTPUT “Enter a number”
INPUT number
IF number < 3
THEN
Result number * 8
ELSE
IF number > 5
THEN
Result number * 2
ELSE
Result <- number * 4
ENDIF
END IF
OUTPUT “The result is “, Result
Number 2:
OUTPUT “Enter a number”
INPUT number
CASE OF number
< 6: Result number/2
> 10: Result number/10
OTHERWISE: Result number/5
ENDCASE
OUTPUT “The result is “, Result
OR
OUTPUT “Enter a number”
INPUT number
CASE OF number
< 6: Result number/2
>=6 AND <=10: Result 5
>10: Result number/10
ENDCASE
OUTPUT “The result is “, Result
MULTIPLICATION TABLE USING LOOPS
Method 1:
Count 1
INPUT number
REPEAT
PRINT “5 x “, Count, “ = “, Count * 5
Count Count + 1
UNTIL Count > number
Method 2:
FOR Count 1 TO 12
PRINT “5 x “, Count, “ = “, Count * 5
NEXT Count
EXAMPLE 4:
START
OUTPUT “Enter a
number”
INPUT number
Is number
<100? Yes
No
Is number
<= 200? No
Yes
OUTPUT “Correct!
STOP
Valid number”
PSEUDOCODE ALGORITHM FOR EXAMPLE 4
OUTPUT ”Enter a number”
INPUT number
WHILE number < 100 OR number > 200 DO
OUTPUT ”Invalid number. Try again!”
INPUT number
ENDWHILE
OUTPUT ”Valid number”
OR
REPEAT
OUTPUT “Enter a number”
INPUT number
IF number < 100 OR number > 200 THEN
OUTPUT “Invalid number”
END IF
UNTIL number >= 100 AND number <=200
OUTPUT ”Valid number”
EXAMPLE 5(a): An algorithm needs to take in three numbers and output the largest. Draw the flowchart for this
algorithm.
PSEUDOCODE
Max 0
FOR Count 1 TO 3
OUTPUT “Enter a number”
INPUT value
IF value > Max THEN
Max value
ENDIF
NEXT Count
OUTPUT Max
EXAMPLE 5(b)
START
max 0
counter 1
Is counter <=
3? No
OUTPUT max
Yes
INPUT num
Is num >
max?
No
Yes
counter max num STOP
counter + 1
The Pseudocode for EXAMPLE 5
An algorithm needs to take in three numbers and output the smallest.
INPUT smallest
FOR count 1 TO 3
OUTPUT “Enter a number”
INPUT num
IF num < smallest THEN
smallest num
END IF
NEXT count
OUTPUT smallest
OR
smallest 100
count 1
WHILE count<=3 DO
OUTPUT “Enter a number”
INPUT number
IF number < smallest THEN
smallest number
ENDIF
count count + 1
ENDWHILE
OUTPUT “The smallest number is “ & smallest
Example 5(b)
A program will:
• input 50 unique integer values
• output the smallest value
• output the average of the values excluding the smallest value.
Draw a program flowchart to represent the algorithm.
START
Min 99
Count 1 Count Count +
Total 0 1
Is Total Total + No
No INPUT Is Number
Count Number
Number < Min
> 50?
Yes
Yes
Min Number
Average (Total –
Min)/50
OUTPUT Min,
Average
STOP
EXAMPLE 6:
num & “X” & count & “ = ” & num*count
count count + 1
EXAMPLE 7:
OUTPUT “ How many numbers do you want to output?”
INPUT number
counter 1
WHILE counter <= number DO
OUTPUT counter
count counter + 1
ENDWHILE
EXAMPLE 8:
The average (mean) of a set of numbers can be found by adding the numbers together and
dividing the resulting total by how many numbers there are in the set.
Write an algorithm in pseudocode to enter and find the average of a set of numbers using a REPEAT…UNTIL
loop. Your algorithm must work for different sets of numbers.
......................................................................................
OUTPUT “Enter how many set of numbers you want to find the average”
INPUT setCount
Counter 0
Total 0
REPEAT
OUTPUT “Enter a value”
INPUT value
Total Total + value
Count Count + 1
UNTIL Count = setCount
Average Total/setCount
OUTPUT Average
EXAMPLE 9: (October, 2022 Paper 12)
A programmer is writing an algorithm in pseudocode to represent a company’s payroll system. The system
calculates a worker’s wages before tax by multiplying the number of hours worked by the rate of pay per
hour. A procedure within the main algorithm representing this could be:
PROCEDURE BeforeTax(Hours, Rate)
WagesBeforeTax Hours * Rate
ENDPROCEDURE
There are two stages involved in calculating the wages after tax:
• the amount of tax paid by the worker is calculated by multiplying the WagesBeforeTax by the
rate of tax (35%);
• the amount of tax paid is then subtracted from the WagesBeforeTax.
(a) Write a procedure for calculating the wages after tax, assuming the value of WagesBeforeTax
is passed to it.
ANSWER:
PROCEDURE AfterTax(BeforeTax)
TaxRate 0.35
Tax BeforeTax * TaxRate
WagesAfterTax BeforeTax – Tax
ENDPROCEDURE
(b) The programmer’s algorithm will use the BeforeTax() procedure and the procedure created
in part (a) to calculate and output the wage after tax for each worker.
Complete the algorithm. The statements have been numbered to help you.
ANSWER:
Count 0
INPUT
NumberOfWorkers
REPEAT
INPUT Hour, Rate
CALL BeforeTax(Hours, Rate)
CALL AfterTax(BeforeTax)
OUTPUT “Wage after tax is “& wageAfterTax
Count Count + 1
UNTIL Count = NumberOfWorkers
PRACTICAL ACTIVITY 4.09
PSEUDOCODE SOLUTION:
Continue False
Total 0
WHILE Continue == False DO
OUTPUT "Do you want to perform a calculation?"
INPUT Response
Response upper(Response)
WHILE Response != "YES" OR Response != "NO"
OUTPUT "Invalid response. Try again!!"
INPUT Response
Response upper(Response)
END WHILE
IF Response == "YES" THEN
OUTPUT "How many numbers do you want to add together?"
INPUT numCount
FOR Count 1 TO numCount
OUTPUT "Enter a number: "
INPUT num
Total Total + num
NEXT Count
ELSE
Continue True
ENDIF
END WHILE
OUTPUT "The total is " & Total
Exercise:
SOLUTION:
alphabets = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z"]
position 0
i0
word input("Enter a
word:") wordLength
len(word)
WHILE (i < wordLength):
position
[Link](word[i])+1
print(word[i]*position)
ii+1
ENDWHILE
Example 10: PRACTICAL ACTIVITY 4.06 (No. 4)
Method 1:
secretNum 8
OUTPUT ”Guess the secret number”
INPUT guess
count 0
WHILE guess <> secretNum DO
OUTPUT ”Guess the secret number”
INPUT guess
count count + 1
ENDWHILE
OUTPUT “Bravo! You are correct. It took you” & count & “guesses.”
Method 2:
secretNum 8
correct False
count 0
WHILE (correct == False) DO
PRINT "Enter a number"
INPUT guess
IF guess == secretNum THEN
correct True
PRINT(“Bravo! You’re correct!”)
count count + 1
ENDWHILE
PRINT ”It took you” & count & “guesses.”
Practical activity 4.07 (No. 2)
OUTPUT” How many letters do you want to enter”
INPUT numLetters
word “”
FOR count 0 TO numLetters-1
OUTPUT”Enter a letter”
INPUT letter
word word + letter
NEXT count
OUTPUT word
Practical activity 4.07 (No. 3)
OUTPUT” Enter your birth day”
INPUT birthDay
OUTPUT” Enter your birth month”
INPUT birthMonth
OUTPUT” Enter your birth year”
INPUT birthYear
birthDate birthday &”/”& birthMonth&”/”& birthyear
OUTPUT ”Your birth date is “& birthdate