0% found this document useful (0 votes)
49 views10 pages

Pseudocode Exercises - SOLVED

Uploaded by

murenzidan1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views10 pages

Pseudocode Exercises - SOLVED

Uploaded by

murenzidan1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Glory academy

L4 SWD

Data structures and Algorithms

Marking scheme of Exercises on Algorithm – Pseudocode

Question 1: Write pseudocode for a program that takes a number as input and prints
whether it is even or odd.

START

INPUT number

IF number MOD 2 == 0 THEN

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

INPUT list of numbers

largest = list[0]

FOR each number IN list DO

IF number > largest THEN

largest = number

END IF

END FOR

PRINT "Largest number is", largest

END

Question 4: Write pseudocode for a program that counts the occurrences of each letter
in a string.

START

INPUT string

CREATE an empty dictionary frequency

FOR each character IN string DO

IF character IN frequency THEN

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

INPUT sorted_array, target

low = 0

high = length of sorted_array - 1

WHILE low <= high DO

mid = (low + high) // 2

IF sorted_array[mid] == target THEN

PRINT "Element found at index", mid

EXIT

ELSE IF sorted_array[mid] < target THEN

low = mid + 1

ELSE

high = mid - 1

END IF

END WHILE

PRINT "Element not found"

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

PRINT "1. Check Balance"


PRINT "2. Deposit"

PRINT "3. Withdraw"

PRINT "4. Exit"

INPUT choice

IF choice == 1 THEN

PRINT "Balance:", balance

ELSE IF choice == 2 THEN

INPUT deposit

balance = balance + deposit

PRINT "Deposited successfully. New Balance:", balance

ELSE IF choice == 3 THEN

INPUT withdraw

IF withdraw <= balance THEN

balance = balance - withdraw

PRINT "Withdrawal successful. New Balance:", balance

ELSE

PRINT "Insufficient balance"

END IF

ELSE IF choice == 4 THEN

PRINT "Thank you. Goodbye!"

EXIT

ELSE

PRINT "Invalid choice. Try again."

END IF
END WHILE

END

Question 7: Write pseudocode to reverse a string input by the user.

START

INPUT string

reversed_string = ""

FOR i = LENGTH(string) - 1 DOWNTO 0 DO

reversed_string = reversed_string + string[i]

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

PRINT "Factorial is", factorial

END

Question 9: Write pseudocode to find the second largest number in a list of integers.

START

INPUT list of numbers

largest = -INFINITY
second_largest = -INFINITY

FOR each number IN list DO

IF number > largest THEN

second_largest = largest

largest = number

ELSE IF number > second_largest AND number != largest THEN

second_largest = number

END IF

END FOR

PRINT "Second largest number is", second_largest

END

Question 10: Write pseudocode for a program that checks if a given word is a
palindrome.

START

INPUT word

reversed_word = ""

FOR i = LENGTH(word) - 1 DOWNTO 0 DO

reversed_word = reversed_word + word[i]

END FOR

IF word == reversed_word THEN

PRINT "Palindrome"

ELSE

PRINT "Not a Palindrome"

END IF

END
Question 11: Write pseudocode for implementing a queue using an array.

START

DEFINE queue as an empty array

DEFINE front = -1, rear = -1, size = 5

FUNCTION ENQUEUE(element):

IF rear == size - 1 THEN

PRINT "Queue is full"

ELSE

IF front == -1 THEN

front = 0

END IF

rear = rear + 1

queue[rear] = element

END IF

FUNCTION DEQUEUE():

IF front == -1 OR front > rear THEN

PRINT "Queue is empty"

ELSE

PRINT "Dequeued element:", queue[front]

front = front + 1

END IF

FUNCTION DISPLAY():

IF front == -1 OR front > rear THEN


PRINT "Queue is empty"

ELSE

FOR i = front TO rear DO

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

IF array[j] > array[j+1] THEN

temp = array[j]

array[j] = array[j+1]

array[j+1] = temp

END IF

END FOR

END FOR

PRINT "Sorted Array:", array

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

PRINT "GCD is", a

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

total = total + grade

END FOR

average = total / number_of_students

PRINT "Class Average Grade:", average

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

WHILE LENGTH(list_of_students) > 0 DO

PRINT "Group", group_number, ":", TAKE first group_size students from list_of_students

REMOVE first group_size students from list_of_students

group_number = group_number + 1

END WHILE

END

You might also like