Pseudocode Writing Practice
Unit 4 - Algorithms
Pseudocode Conventions Reference
Variables & I/O
- variable = value
- INPUT variable
- OUTPUT message
Selection
- IF condition THEN
- ELSE IF condition THEN
- ELSE
- ENDIF
Iteration (Loops)
- FOR i = start TO end
- NEXT i
- WHILE condition
- ENDWHILE
- REPEAT ... UNTIL
Operators
- = (equal), <> (not equal)
- <, >, <=, >=
- AND, OR, NOT
- MOD (remainder)
Question 1: Even or Odd (2 marks)
Write pseudocode that asks the user for a number and outputs whether it is "Even" or "Odd".
Hint: Use the MOD operator (number MOD 2 = 0 means even)
Question 2: Grade Calculator (3 marks)
Write pseudocode that asks for a test score (0-100) and outputs the grade:
- - 80-100: "A"
- - 60-79: "B"
- - 40-59: "C"
- - Below 40: "Fail"
Question 3: Countdown Timer (3 marks)
Write pseudocode that asks the user for a starting number, then counts down to 0, outputting each number. When it reaches 0, output "Blast off!".
Question 4: Average Calculator (4 marks)
Write pseudocode that asks the user to enter 5 numbers, calculates their average, and outputs the result.
Question 5: Password Validation (4 marks)
Write pseudocode for a login system that:
- - Has a stored password of "secret123"
- - Gives the user 3 attempts to enter the correct password
- - Outputs "Access granted" if correct
- - Outputs "Account locked" after 3 failed attempts
Question 6: Find Maximum (5 marks)
Write pseudocode that asks the user to enter numbers until they enter -1 (sentinel value).
Then output the largest number that was entered.
Hint: Keep track of the maximum as you go
Challenge Question: Linear Search (6 marks)
Write pseudocode for a linear search algorithm that:
- - Has an array called "names" with 5 names
- - Asks the user for a name to search for
- - Searches through the array to find the name
- - Outputs the position if found, or "Name not found" if not
View Sample Solutions
INPUT number
IF number MOD 2 = 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
ENDIF INPUT score
IF score >= 80 THEN
OUTPUT "A"
ELSE IF score >= 60 THEN
OUTPUT "B"
ELSE IF score >= 40 THEN
OUTPUT "C"
ELSE
OUTPUT "Fail"
ENDIF INPUT start
count = start
WHILE count > 0
OUTPUT count
count = count - 1
ENDWHILE
OUTPUT "Blast off!" total = 0
FOR i = 1 TO 5
INPUT number
total = total + number
NEXT i
average = total / 5
OUTPUT average password = "secret123"
attempts = 0
valid = FALSE
REPEAT
INPUT guess
attempts = attempts + 1
IF guess = password THEN
valid = TRUE
ENDIF
UNTIL valid = TRUE OR attempts = 3
IF valid THEN
OUTPUT "Access granted"
ELSE
OUTPUT "Account locked"
ENDIF INPUT number
max = number
WHILE number -1
IF number > max THEN
max = number
ENDIF
INPUT number
ENDWHILE
OUTPUT max names = ["Alice", "Bob", "Charlie", "David", "Eve"]
INPUT searchName
found = FALSE
position = -1
FOR i = 0 TO 4
IF names[i] = searchName THEN
found = TRUE
position = i
ENDIF
NEXT i
IF found THEN
OUTPUT "Found at position " + position
ELSE
OUTPUT "Name not found"
ENDIF