Pseudocode Exercises - SOLVED
Pseudocode Exercises - SOLVED
L4 SWD
Question 1: Write pseudocode for a program that takes a number as input and prints
whether it is even or odd.
START
INPUT number
PRINT "Even"
ELSE
PRINT "Odd"
END IF
END
Question 2: Write pseudocode for a program that calculates the sum of numbers from 1
to N (where N is user input).
START
INPUT N
sum = 0
FOR i = 1 TO N DO
sum = sum + i
END FOR
PRINT sum
END
Question 3: Write pseudocode to find the largest number in a list of integers.
START
largest = list[0]
largest = number
END IF
END FOR
END
Question 4: Write pseudocode for a program that counts the occurrences of each letter
in a string.
START
INPUT string
frequency[character] = frequency[character] + 1
ELSE
frequency[character] = 1
END IF
END FOR
PRINT frequency
END
Question 5: Write pseudocode for a binary search algorithm to find an element in a
sorted array.
START
low = 0
EXIT
low = mid + 1
ELSE
high = mid - 1
END IF
END WHILE
END
Question 6: Write pseudocode to simulate a simple ATM system with options to check
balance, deposit, and withdraw money.
START
balance = 1000
WHILE True DO
INPUT choice
IF choice == 1 THEN
INPUT deposit
INPUT withdraw
ELSE
END IF
EXIT
ELSE
END IF
END WHILE
END
START
INPUT string
reversed_string = ""
END FOR
PRINT reversed_string
END
Question 8: Write pseudocode for a program that calculates the factorial of a number.
START
INPUT number
factorial = 1
FOR i = 1 TO number DO
factorial = factorial * i
END FOR
END
Question 9: Write pseudocode to find the second largest number in a list of integers.
START
largest = -INFINITY
second_largest = -INFINITY
second_largest = largest
largest = number
second_largest = number
END IF
END FOR
END
Question 10: Write pseudocode for a program that checks if a given word is a
palindrome.
START
INPUT word
reversed_word = ""
END FOR
PRINT "Palindrome"
ELSE
END IF
END
Question 11: Write pseudocode for implementing a queue using an array.
START
FUNCTION ENQUEUE(element):
ELSE
IF front == -1 THEN
front = 0
END IF
rear = rear + 1
queue[rear] = element
END IF
FUNCTION DEQUEUE():
ELSE
front = front + 1
END IF
FUNCTION DISPLAY():
ELSE
PRINT queue[i]
END FOR
END IF
END
Question 12: Write pseudocode to sort an array using the Bubble Sort algorithm.
START
INPUT array
n = LENGTH(array)
FOR i = 0 TO n-1 DO
FOR j = 0 TO n-i-2 DO
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
END IF
END FOR
END FOR
END
Question 13: Write pseudocode for calculating the GCD (Greatest Common Divisor) of
two numbers using the Euclidean algorithm.
START
INPUT a, b
WHILE b != 0 DO
temp = b
b = a MOD b
a = temp
END WHILE
END
Question 14: Write pseudocode for a program that calculates the average grade of a
class.
START
INPUT number_of_students
total = 0
FOR i = 1 TO number_of_students DO
INPUT grade
END FOR
END
Question 15: Write pseudocode to randomly assign students to study groups.
START
INPUT list_of_students
INPUT group_size
SHUFFLE list_of_students
group_number = 1
PRINT "Group", group_number, ":", TAKE first group_size students from list_of_students
group_number = group_number + 1
END WHILE
END