In: Computer Science
Examples
Example 1: Write pseudo code that reads two numbers
and multiplies them together and print out their product. Example
2: Write pseudo code that tells a user that the number they entered
is not a 5 or a 6.
Example 3: Write pseudo code that performs the
following: Ask a user to enter a number. If the number is between
0
and 10, write the word blue. If the number is between
10 and 20, write the word red. if the number is between
20 and 30, write the word green. If it is any other
number, write that it is not a correct color option. Example 4:
Write pseudo code to print all multiples of 5 between 1 and 100
(including both 1 and 100). Example 5: Write pseudo code that will
count all the even numbers up to a user defined stopping point.
Example 6: Write pseudo code that will perform the
following.
a) Read in 5 separate numbers.
b) Calculate the average of the five
numbers.
c) Find the smallest (minimum) and largest (maximum)
of the five entered numbers.
d) Write out the results found from steps b and c with
a message describing what they are
Homework 1: Write pseudo code that reads in three
numbers and writes them all in sorted order.
Homework 2: Write pseudo code that will calculate a
running sum. A user will enter numbers that will be added to
the
sum and when a negative number is encountered, stop
adding numbers and write out the final result
Pseudocode is an informal way of communicating your algorithm for any computation problem that does not require any specific knowledge of syntax of any programming language. As We know, Once you know the algorithm, you can implement in with any programming language. Many interviewers ask for pseudocode rather than asking to write a program with a proper language syntax. This way they want to check your problem solving skills.
For the below pseudocodes // and /*---*/ represent
comments.
Example 1:
Begin:
Read: x , y // Read two numbers in x and y
Set: Product = x * y
Print: product
End
Example 2:
Begin:
Read: x
if x != 5 or x != 6 then // != represent not
equal
Print: It's not a 5 or 6
else
Print: It's a 5 or 6
End
Example 3:
Begin:
Read: x
if x >= 0 and x < 10 then
Print: Blue
else if x >= 10 and x < 20
Print: Red
else if x >= 20 and x < 30
Print: Green
else
Print: Not a correct color
option
End
Example 4:
/* Start from 5 to 100 and increment by 5 everytime and print
it. We could have started the loop from 1 as well and incrementing
it 1 each time and check if it is a multiple of 5 or not, but that
process will be slow*/
Begin:
for i = 5 to 100 by 5
Print: i and goto new line
End
Or,
Begin:
for i = 5; i<=100; i = i + 5
Print: i and goto new line
End
Example 5:
Solution (a)
Begin:
Read: x
Initialize: count_of_even = 0
for i=2 to x by 2
count_of_even = count_of_even +
1
Print: count_of_even
End
Solution (b)
Begin:
Read: x
Initialize: count_of_even = 0
for i = 2; i <= x; i = i+1
if i % 2 == 0
count_of_even =
count_of_even + 1
Print: count_of_even
End
Example 6:
Begin:
// Suppose A[0:4] represent an array of 5 numbers and
Array index starts from 0 thus it will be A[0], A[1], A[2], A[3],
A[4]
Initialize: A[0:4]
for i = 0 to 4 by 1:
Read: A[i]
Initialize: sum = 0, minimum = A[0], maximum = 0
for i = 0 to 4 by 1:
sum = sum + A[i]
if A[i] < minimum
minimum =
A[i]
if A[i] > maximum
maximum =
A[i]
Print: "Average will be": sum/5
Print: "Minimum of Array": minimum
Print: "Maximum of Array": maximum
End
Homework 1:
Begin:
Read: a, b, c
// Since there are three numbers We can just check and
swap their positions based on their values
if a > b
swap a <=> b
if a > c
swap a <=> c
// By this time you know that minimum number would
have been assigned to a, now we just need to compare b and c
if b > c
swap b <=> c
Print: a, b, c
End
Homework 2:
//Below pseudocode implements a while loop which means keep
continuing the loop until your condition is satisfied
Begin:
Initialize: sum=0
Read: x
while x >= 0:
sum = sum + x
Read: x
//while loop will exit as soon as you enter a number
which is less than 0. It will not even go inside while loop if the
first number you enter is 0.
Print: sum
End