Pseudocode Examples
Pseudocode Examples
o Add Two Numbers.(Simple Pseudocode Example) o Calculating Class Average Using While Loop
o Calculate Area and Perimeter of Rectangle (Simple Pseudocode
Pseudocode Example) o Calculating Average of User-Entered Grades with
o Find Area and Perimeter of a Square (Simple Sentinel Value Pseudocode
Pseudocode Example) o Student passed and failured in pseudocode
o Find Area Of Circle using Radius (Simple o Pseudocode of Bubble sort technique
Pseudocode Example) o Pseudocode Example: Defining a Function with FOR
o Find Perimeter Of Circle using Radius (Simple Loop and IF Statement
Pseudocode Example) o QuickSort Algorithm using Function Declaration in
o Calculate sales taxes (Simple Pseudocode Example) Pseudocode
o Solve Quadratic Equation (Pseudocode If Else For example, the following is a simple algorithm
Example) written in pseudocode to add two numbers together:
o Issue for driver licence (Pseudocode If Else Example) 1
o Check a Number is Positive or 2PROCEDURE AddNumbers(x, y)
Negative (Pseudocode If Else Example) 3 SET sum = 0
o Find the biggest of three (3) Numbers (Pseudocode If 4 sum = x + y
Else Example) 5 RETURN sum
o Print Numbers from 1 to 100. (Pseudocode For Loop 6
Example) Add Two Numbers (Simple Pseudocode Example)
o Find Sum of Natural Numbers (1 to 1
100). (Pseudocode For Loop Example) 2 BEGIN
o Read 50 numbers and find their sum and 3 NUMBER s1, s2, sum
average. (Pseudocode For Loop Example) 4 OUTPUT("Input number1:")
o Read 10 numbers and find sum of even numbers. 5 INPUT s1
(Pseudocode For Loop Example) 6 OUTPUT("Input number2:")
o Find the sum of all elements of array. (Pseudocode 7 INPUT s2
For Loop Example) 8 sum=s1+s2
o Calculate square of a number (Simple Pseudocode 9 OUTPUT sum
Example) 10END
o Calculate the Square Root of a Number (Pseudocode 11
For Loop Example) This pseudocode describes a program that takes two
o Swap two variables with using a temporary numbers as input from the user, adds them together
variable (Simple Pseudocode Example) and outputs the sum.
o Swap two variables without using a temporary 1. The variables “s1”, “s2”, and “sum” are declared.
variable (Simple Pseudocode Example) 2. The program outputs the message “Input
o Print Numbers from 1 to n. (Pseudocode For Loop number1:” and waits for the user to input a value,
Example) which is stored in “s1”.
o Calculate the Sum and Average of n Number. 3. The program outputs the message “Input
(Pseudocode For Loop Example) number2:” and waits for the user to input a value,
o Design the algorithm and flowchart that finds and which is stored in “s2”.
display the larger of the two numbers given different 4. The value of “sum” is calculated as the sum of “s1”
from each other. and “s2”.
o Perform the application that calculates the area of 5. The value of “sum” is outputted as the result of the
the triangle whose height and base length entered by program.
the keyboard. Calculate Area and Perimeter of Rectangle
o The voltage (V) between the poles of a conductor is 1
equal to the product of the current (I) passing 2 BEGIN
through the conductor and the resistance (R) present 3 NUMBER b1,b2,area,perimeter
on the conductor. It’s demonstrated by the V = I * R 4 INPUT b1
formula. 5 UNPUT b2
o What is the algorithm that the number entered by 6 area=b1*b2
the user from the keyboard fully divided if it is 7 perimeter=2*(b1+b2)
divided into 3 and 5 . 8 OUTPUT area
o What is the algorithm that finds and shows on the 9 OUTPUT perimeter
screen that the water is in the form of solid, liquid or 10END
gaseous according to the temperature? 11
o While calculating the wage of a worker at a factory, This pseudocode describes a program that calculates
these criterias are complied; the area and perimeter of a rectangle given its length
o Program to Find GCD of Two Numbers and width.
o The LCM of two integers n1 and n2 is the smallest 1. The variables “b1”, “b2”, “area”, and “perimeter”
positive integer that is perfectly divisible by are declared.
both n1 and n2 (without a remainder)
2. The program prompts the user to input the length Find Perimeter Of Circle using Radius (Simple
of the rectangle and stores the value in “b1”. Pseudocode Example)
3. The program prompts the user to input the width 1
of the rectangle and stores the value in “b2”. 2BEGIN
4. The area of the rectangle is calculated by multiplying 3NUMBER r, perimeter
“b1” and “b2” and stored in the “area” variable. 4INPUT r
5. The perimeter of the rectangle is calculated by 5perimeter=2*3.14*r
multiplying 2 by the sum of “b1” and “b2” and 6OUTPUT perimeter
stored in the “perimeter” variable. 7END
6. The program outputs the value stored in the “area” 8
variable. This pseudocode describes a program that calculates
7. The program outputs the value stored in the the circumference of a circle given its radius.
“perimeter” variable. 1. The variables “r” and “perimeter” are declared.
Find Area and Perimeter of a Square (Simple 2. The program prompts the user to input the radius
Pseudocode Example) of the circle and stores the value in “r”.
1 3. The circumference of the circle is calculated by
2 BEGIN multiplying 2 by pi (3.14) and the radius and
3 NUMBER len, area,perimeter storing the result in the “perimeter” variable.
4 INPUT len 4. The program outputs the value stored in the
5 area = len*len “perimeter” variable.
6 perimeter = len*4 Calculate sales taxes (Simple Pseudocode Example)
7 OUTPUT area 1
8 OUTPUT perimeter 2 BEGIN
9 END 3 NUMBER price, tax, taxRate, total
10 4
This pseudocode describes a program that calculates 5 OUTPUT "Enter Product Price"
the area and perimeter of a square given its length of 6 INPUT price
one side. 7 OUTPUT "Enter tax rate amoung 1 and 100"
1. The variables “len”, “area”, and “perimeter” are 8 OKU taxRate
declared. 9
2. The program prompts the user to input the length 10tax= price* taxRate/100
of one side of the square and stores the value in 11total= price + tax
“len”. 12
3. The area of the square is calculated by squaring the 13OUTPUT "Product tax="+tax
length of one side and storing the result in the 14OUTPUT "Product total price ="+total
“area” variable. 15
4. The perimeter of the square is calculated by 16END
multiplying the length of one side by 4 and stored 17
in the “perimeter” variable. This pseudocode describes a program that calculates
5. The program outputs the value stored in the “area” the total cost of a product, including tax, based on the
variable. price of the product and the tax rate.
6. The program outputs the value stored in the 1. The variables “price”, “tax”, “taxRate”, and “total”
“perimeter” variable. are declared.
Find Area Of Circle using Radius (Simple 2. The program outputs the message “Enter Product
Pseudocode Example) Price” and prompts the user to input the price of
1 the product.
2BEGIN 3. The program outputs the message “Enter tax rate
3NUMBER r, area amoung 1 and 100” and prompts the user to input
4INPUT r the tax rate as a percentage.
5area=3.14*r*r 4. The tax amount is calculated by multiplying the
6OUTPUT area price of the product by the tax rate divided by 100
7END and stored in the “tax” variable.
8 5. The total cost of the product, including tax, is
This pseudocode describes a program that calculates calculated by adding the price of the product and the
the area of a circle given its radius. tax amount and stored in the “total” variable.
1. The variables “r” and “area” are declared. 6. The program outputs the message “Product tax=”
2. The program prompts the user to input the radius followed by the value stored in the “tax” variable.
of the circle and stores the value in “r”. 7. The program outputs the message “Product total
3. The area of the circle is calculated by multiplying price =” followed by the value stored in the “total”
pi (3.14) by the square of the radius and storing the variable.
result in the “area” variable.
4. The program outputs the value stored in the “area” Solve Quadratic Equation
variable. 1
2 BEGIN 3. The program checks if “age” is greater than or equal
3 NUMBER a, b, c, d, x1, x2 to 16.
4 INPUT a,b,c 4. If “age” is greater than or equal to 16, the program
5 d = b^2-4ac outputs the message “You can take driving licence”.
6 IF (d >= 0) THEN 5. If “age” is less than 16, the program outputs the
7 x1 = (-b+√d)/2a yada x1 = (-b+d^(1/2)/2a message “You can’t take driving licence”.
8 x2 = (-b-√d)/2a yada x2 = (-b-d^(1/2)/2a Check a Number is Positive or Negative
9 OUTPUT "ROOT 1:"+x1 1
10 OUTPUT "ROOT 2:"+x2 2 BEGIN
11 ELSE IF (d == 0) THEN 3
12 x1=x2= -b/2a 4 NUMBER num
13 OUTPUT "ROOT 1:"+x1 5
14 OUTPUT "ROOT 2:"+x2 6 OUTPUT "Enter a Number"
15ELSE 7 OKU num
16 OUTPUT "There is no real root" 8
17ENDIF 9 IF num>0 THEN
18END 10 OUTPUT "Entered number is positive"
19 11ELSE IF num <0 THEN
This pseudocode describes a program that solves the 12 OUTPUT "Entered number is negative"
quadratic equation ax^2 + bx + c = 0 and outputs its 13ELSE
real roots. 14 OUTPUT "Entered number is zero"
1. The variables “a”, “b”, “c”, “d”, “x1”, and “x2” are 15ENDIF
declared. 16
2. The program prompts the user to input the values 17END
of the coefficients “a”, “b”, and “c”. 18
3. The value of “d”, the discriminant, is calculated as This pseudocode describes a program that determines if
the square of “b” minus 4 times “a” times “c”. a number entered by the user is positive, negative, or
4. If “d” is greater than or equal to 0, then the zero.
program calculates the two real roots of the 1. The variable “num” is declared.
equation using the formula (-b±√d) / 2a and stores 2. The program prompts the user to enter a number
the values in “x1” and “x2”. and stores the value in “num”.
5. If “d” is equal to 0, then the program calculates the 3. The program checks if “num” is greater than zero.
one real root of the equation using the formula -b / 4. If “num” is greater than zero, the program outputs
2a and stores the value in both “x1” and “x2”. the message “Entered number is positive”.
6. If “d” is less than 0, then the program outputs the 5. If “num” is less than zero, the program outputs the
message “There is no real root”. message “Entered number is negative”.
7. The program outputs the message “ROOT 1:” 6. If “num” is equal to zero, the program outputs the
followed by the value stored in “x1” and the message “Entered number is zero”.
message “ROOT 2:” followed by the value stored Find the biggest of three (3) Numbers
in “x2”. 1
Issue for driver licence (Pseudocode If Else Example) 2 BEGIN
1 3
2 BEGIN 4 NUMBER num1,num2,num3
3 NUMBER age 5
4 6 INPUT num1
5 INPUT "Enter your age for driving licence" 7 INPUT num2
6 OUTPUT age 8 INPUT num3
7 9
8 IF age>=16 THEN 10 IF num1>num2 AND num1>num3 THEN
9 OUTPUT "You can take driving licence" 11 OUTPUT num1+ "is higher"
10ELSE 12 ELSE IF num2 > num3 THEN
11 OUTPUT "You can't take driving licence" 13 OUTPUT num2 + "is higher"
12ENDIF 14 ELSE
13 15 OUTPUT num3+ "is higher"
14END 16 ENDIF
15 17
This pseudocode describes a program that determines if 18END
a person is eligible for a driving license based on their 19
age. This pseudocode describes a program that determines
1. The variable “age” is declared. the highest of three numbers entered by the user.
2. The program prompts the user to enter their age and 1. The variables “num1”, “num2”, and “num3” are
stores the value in “age”. declared.
2. The program prompts the user to enter three 6 FOR counter=1 TO 50 STEP counter DO
numbers and stores the values in “num1”, “num2”, 7 OUTPUT "Enter a Number"
and “num3”. 8 INPUT num
3. The program checks if “num1” is greater than both 9 sum=sum+num
“num2” and “num3”. 10ENDFOR
4. If “num1” is greater than both “num2” and 11
“num3”, the program outputs the message “num1 12OUTPUT sum
is higher”. 13
5. If “num1” is not greater than both “num2” and 14END
“num3”, the program checks if “num2” is greater 15
than “num3”. This pseudocode initializes a variable
6. If “num2” is greater than “num3”, the program called counter and sum with a value of 0. Then it starts
outputs the message “num2 is higher”. a loop, FOR loop that runs 50 times with a step
7. If “num2” is not greater than “num3”, the program of counter, meaning it increases the counter by itself at
outputs the message “num3 is higher”. each iteration. In each iteration, the code prompts the
Print Numbers from 1 to 100. user to enter a number, and stores it in the
1 variable num. The num is then added to the sum and
2 BEGIN the loop continues until it runs 50 times. Finally, the
3 NUMBER counter code outputs the final value of sum.
4 Read 10 numbers and find sum of even numbers.
5 FOR counter = 1 TO 100 STEP 1 DO 1
6 OUTPUT counter 2 BEGIN
7 ENDFOR 3
8 4 NUMBER counter, sum=0, num
9 END 5
10 6 FOR counter=1 TO 10 STEP 1 DO
This pseudocode defines a loop that outputs the 7 OUTPUT "Enter a Number"
numbers from 1 to 100, incrementing the counter by 1 8 INPUT num
each time. The loop starts with the declaration of a 9
variable “counter” as the loop index. The loop structure 10 IF num % 2 == 0 THEN
is then defined using the “FOR” keyword followed by 11 sum=sum+num
the starting value of “counter” (1), the final value of 12 ENDIF
“counter” (100), and the step size (1). The loop 13ENDFOR
continues to execute as long as “counter” is less than or 14OUTPUT sum
equal to 100. In each iteration, the value of “counter” is 15
outputted using the “OUTPUT” keyword. The loop 16END
ends with the “ENDFOR” keyword. 17
Find Sum of Natural Numbers (1 to 100). (Pseudocode This pseudocode calculates the sum of only even
For Loop Example) numbers from 10 numbers entered by the user. It starts
1 with declaring two variables: counter and sum, and sets
2 BEGIN the value of sum to 0. The code then enters a for loop
3 that runs 10 times (i.e., from 1 to 10). In each iteration of
4 NUMBER counter, sum=0 the loop, the code prompts the user to “Enter a
5 Number”. The user input is stored in the variable num.
6 FOR counter=1 TO 100 STEP 1 DO Then, the code checks whether num is an even number
7 sum=sum+counter by checking if num divided by 2 has a remainder of 0. If
8 ENDFOR the result of this check is true (i.e., num is even), the
9 OUTPUT sum code adds num to sum. After 10 iterations of the loop,
10 the code outputs the final value of sum.
11END Find the sum of all elements of array.
12 1
This pseudocode defines a simple program to calculate 2 BEGIN
the sum of numbers from 1 to 100. The program uses a 3 NUMBER i=0, n=5, sum=0
“FOR” loop to iterate through each number from 1 to 4 ARRAY numbers={65,45,10,7,125}
100 and adds each number to a running sum. The final 5 FOR i=0 TO n-1 STEP 1 DO
result, the sum of all the numbers from 1 to 100, is 6 sum = sum + numbers[i]
output at the end of the program. 7 ENDFOR
Read 50 numbers and find their sum and average. 8
1 9 OUTPUT "Sum of numbers in the array"+sum
2 BEGIN 10
3 11END
4 NUMBER counter, sum=0, num 12
5
This pseudocode is a program that calculates the sum of On each iteration of the loop, num is added
the numbers in an array. The array “numbers” has 5 to result, resulting in the running sum being equal
elements, 65, 45, 10, 7, and 125. The program uses a for to num multiplied by itself after num iterations.
loop to iterate through the elements of the array, Finally, the power of the number is output
starting from the first element (index 0) to the last as result.
element (index n-1) with a step of 1. At each iteration, Calculate the Square Root of a Number
the current element of the array is added to the “sum” 1
variable. Finally, the program outputs the message 2 BEGIN
“Sum of numbers in the array” followed by the value of 3
the “sum” variable. 4 NUMBER root=1, counter=0,num
Calculate square of a number 5
Alternative 1: 6 OUTPUT "Enter a number for calculate the root"
1 7 INPUT num
2 BEGIN 8
3 NUMBER num, result 9 WHILE sayac < sayi+1 THEN
4 OUTPUT "Enter a number for calculate the power of 10 i=i+1
5 the number" 11 root=(num/root+root)/2
6 INPUT num 12END WHILE
7 13
8 result=num*num 14OUTPUT root
9 15
10OUTPUT "Power of the Number:" + result 16END
11 17
12END This pseudocode describes a program to calculate the
square root of a number.
This pseudocode calculates the power of a number by The variables root, counter, and num are declared and
squaring it. initialized as follows:
The steps of the pseudocode are: root is initialized to 1
1. It declares a NUMBER variable named “num” and counter is initialized to 0
“result”. num is input by the user
2. It prompts the user to “Enter a number for calculate The program then enters a while loop where counter is
the power of the number”. compared to num + 1. In each iteration of the loop:
3. The user inputs a value for “num”. counter is incremented by 1
4. The value of “result” is calculated as the square of
root is updated using the formula root = (num/root
“num” by multiplying “num” by itself.
+ root)/2
5. The final result “Power of the Number” is displayed
Once counter is greater than or equal to num + 1, the
along with its value.
while loop terminates and the final value of root is
Alternative 2:
output as the result.
1
Swap two variables with using a temporary variable
2 BEGIN
1
3 NUMBER num, result=0,counter
2 BEGIN
4
3 NUMBER a,b,c
5 OUTPUT "Enter a number for calculate the power of
4 a=10, b=20
6 the number"
5 OUTPUT "Value of a :"+a
7 INPUT num
6 OUTPUT "Value of b :"+b
8
7
9 FOR counter=0 TO num-1 STEP 1 DO
8 c=a
10 result +=num
9 a=b
11ENDFOR
10b=c
12
11
13OUTPUT "Power of the Number:" + result
12OUTPUT "Value of a :"+a
14
13OUTPUT "Value of b :"+b
15END
14
15END
This pseudocode calculates the power of a given
16
number by repeatedly adding the number to a running
This pseudocode implements a simple swapping of two
sum.
variables, a and b.
A variable num is defined to capture the input
First, the values of a and b are initialized
number from the user.
to 10 and 20, respectively.
A variable result is initialized to zero.
Then, the original values of a and b are displayed
The pseudocode then uses a FOR loop to on the screen.
iterate num times.
After that, a temporary variable c is assigned the
value of a.
The value of a is then updated to be equal to b and 10OUTPUT "Average of numbers :"+avg
the value of b is updated to be equal to the value 11END
of c. 12
Finally, the updated values of a and b are This pseudocode initializes a counter, counter, a
displayed on the screen. sum, sum, and an average, avg, as well as an input n.
Swap two variables without using a temporary Then, using a FOR loop, it iterates
variable from 1 to n (inclusive) by incrementing the counter
1 by 1 on each iteration. At each iteration, the current
2 BEGIN value of counter is added to sum. After the loop, it
3 NUMBER a,b calculates the average of the numbers by
4 a=10, b=20 dividing sum by n. Finally, it outputs the sum and
5 OUTPUT "Value of a :"+a average of the numbers.
6 OUTPUT "Value of b :"+b Design the algorithm and flowchart that finds and
7 display the larger of the two numbers given different
8 a=a+b from each other.
9 b=a-b 1
10a=a-b 2 BEGIN
11 3
12OUTPUT "Value of a :"+a 4 Number n1,n2
13OUTPUT "Value of b :"+b 5
14 6 Input n1
15END 7
16 8 Input n2
This pseudocode block performs the task of swapping 9
two numbers without using a temporary variable. It 10 IF (n1>n2) THEN
declares two variables a and b and assigns 11 OUTPUT(n1+" is higher")
values 10 and 20 to them respectively. Then it outputs 12 ELSE IF(n2>n1)
their values. Then it performs swapping of values by 13 OUTPUT(n2+" is higher")
using the mathematical expression a = a + b and b = a - 14 ELSE
b and a = a - b. At the end, it outputs the swapped 15 OUTPUT("n1=n2")
values of a and b. 16 END IF
Print Numbers from 1 to n. (Pseudocode For Loop 17END
Example) 18
1 This pseudocode takes two inputs, n1 and n2, and then
2 BEGIN it compares their values to determine which one is
3 NUMBER counter,n higher. If n1 is greater than n2, it outputs the
4 INPUT n message n1 is higher. If n2 is greater than n1, it outputs
5 FOR counter = 1 TO n STEP 1 DO the message n2 is higher. If both n1 and n2 are equal, it
6 OUTPUT counter outputs the message n1=n2.
7 ENDFOR Perform the application that calculates the area of the
8 triangle whose height and base length entered by the
9 END keyboard.
10 1
This pseudocode defines a simple program that takes 2 BEGIN
an integer input n and outputs the numbers from 1 3
to n in sequence, incrementing by 1 each time. The 4 Number b,h
program starts by declaring the variables counter and n. 5 ,area
The value of n is then read as input from the user. 6 Input b
Next, a for loop is initiated with the counter starting at 7
1, ending at n, and incrementing by 1 each iteration. 8 Input h
Inside the loop, the value of counter is output on each 9
iteration. 10area=(b*h)/2
Finally, the program ends. 11OUTPUT (Area of Triangle is "+area)
Calculate the Sum and Average of n Number. 12END
1 13
2 BEGIN This pseudocode defines a program that calculates the
3 NUMBER counter,n,sum,avg area of a triangle. It takes two inputs, “b” and “h”,
4 INPUT n which represent the base and height of the triangle
5 FOR counter = 1 TO n STEP 1 DO respectively. It then calculates the area using the
6 sum=sum+counter formula (b * h) / 2, and outputs the result as “Area of
7 ENDFOR Triangle is” followed by the value of the area. The
8 avg=sum/n program uses the input values of “b” and “h” to
9 OUTPUT "Sum of numbers :"+sum
perform the calculation, and the result is stored in the 12
“area” variable. This is a simple pseudocode program that checks the
The voltage (V) between the poles of a conductor is temperature (inputted as “temp”) and outputs whether
equal to the product of the current (I) passing through the substance is in a solid, liquid, or gaseous state based
the conductor and the resistance (R) present on the on the temperature. If the temperature is less than or
conductor. It’s demonstrated by the V = I * R formula. equal to 0, the output will be “solid state.” If the
What is the algorithm of the program that calculates the temperature is less than 100 but greater than 0, the
voltage between the poles of the conductor by using the output will be “liquid state.” If the temperature is
formula according to the current and resistance values greater than or equal to 100, the output will be “gaseous
entered by the user. state.”
1 While calculating the wage of a worker at a factory,
2BEGIN these criteria are complied;
3Number i,r,v If the worker has worked less than 40 hours, the wage is
4Input i calculated by multiplying the hours worked and the
5Input r hourly wage, if the employee has worked for 40 hours
6v=i*r or more, the hours worked calculated as 2 hours. What
7OUTPUT (v) is the algorithm that prints the amount to be paid
8END according to this information?
9 1
This pseudocode calculates the voltage between the 2 BEGIN
poles of a conductor using Ohm’s law, which states that 3 Number hour,fee,total
the voltage between the poles of a conductor is equal to 4 INPUT(hour)
the product of the current passing through the 5 INPUT(fee)
conductor and the resistance present on the conductor. 6 IF(hour<40)
The code takes two inputs, the current (I) passing 7 total=hour*fee
through the conductor and the resistance (R) present on 8 ELSE IF(hour>=40)
the conductor. Then, the voltage (V) is calculated as the 9 total=hour*2*fee
product of the current and the resistance (V = I * R). 1 END IF
Finally, the calculated voltage is outputted. 0 OUTPUT(total)
What is the algorithm that the number entered by the 1 END
user from the keyboard fully divided if it is divided 1
into 3 and 5 . 1
1 2
2BEGIN 1
3Number num 3
4Input num This is a simple pseudocode program that calculates the
5 IF(num%3==0 AND num%5==0) total fee for working a certain number of hours, with
6 OUTPUT(num+ " is divided into 3 and 5") the number of hours and hourly fee as inputs. If the
7END number of hours worked is less than 40, the total fee is
8 calculated by multiplying the number of hours by the
This algorithm takes a number as input from the user hourly fee. If the number of hours worked is equal to or
and checks if it is fully divisible by both 3 and 5. If the greater than 40, the total fee is calculated by multiplying
number is fully divisible by both 3 and 5, the algorithm the number of hours by twice the hourly fee. The final
outputs a message saying that the number is divided total fee is then outputted.
into 3 and 5. The algorithm uses the modulo Program to Find GCD of Two Numbers
operator % to determine if there is a remainder when 1
the number is divided by 3 and 5. If the result of num % 2 BEGIN
3 and num % 5 is 0, it means that the number is fully 3
divisible by both 3 and 5. 4 NUMBER n1 , n2 , gcd = 1, i
What is the algorithm that finds and shows on the 5
screen that the water is in the form of solid, liquid or 6 OUTPUT "Enter first Number:"
gaseous according to the temperature? 7 INPUT n1
1 8
2 BEGIN 9 OUTPUT "Enter second Number:"
3 Number temp 10INPUT n2
4 Input temp 11
5 IF(temp<=0) 12 FOR i = 1; i <= n1 && i <= n2; ++i THEN
6 OUTPUT("solid state") 13
7 ELSE IF(temp<100) 14 IF n1 % i == 0 && n2 % i == 0 THEN
8 OUTPUT("liquid state") 15 gcd = i
9 ELSE 16 END IF
10 OUTPUT("gaseous state") 17END FOR
11END 18
19OUTPUT " G.C.D of "+n1+"and "+n1+" is "+ gcd 3 Print "passed"
20 4else
21END 5 Print "failed"
22 6
This is a pseudocode program that calculates the The pseudocode above represents a simple program
greatest common divisor (GCD) of two numbers, n1 that checks if a student’s grade is greater than or equal
and n2, which are input by the user. The program starts to 60 and prints the result. The program has two
by initializing variables n1, n2, gcd, and i. Then, it branches of execution, each represented by
prompts the user to enter the two numbers. Next, it an IF statement and an accompanying ELSE clause.
uses a for loop to iterate over the range of numbers If the student’s grade is greater than or equal to 60, the
from 1 to the minimum of n1 and n2. For each iteration, first branch of the program is executed and the message
the program checks if the current number i evenly “passed” is printed. If the student’s grade is less than
divides both n1 and n2. If this condition is met, i is 60, the second branch of the program is executed and
stored as the new gcd. This continues until all numbers the message “failed” is printed.
in the range have been checked. Finally, the program This pseudocode provides a clear and concise
outputs the result by concatenating the values of n1, n2, representation of the program’s logic, making it easy to
and gcd in a string and outputting the string. understand and implement. The use of the IF statement
The LCM of two integers n1 and n2 is the smallest and the ELSE clause makes the program easy to read
positive integer that is perfectly divisible by and understand, and the simple use of
both n1 and n2 (without a remainder) the PRINT statement makes the output of the program
1 easy to see.
2 BEGIN Calculating Class Average Using While Loop
3 NUMBER n1 , n2 , lcm; Pseudocode
4 1
5 OUTPUT "Enter first Number:"; 2 Set total to zero
6 INPUT n1 3 Set grade counter to one
7 4
8 OUTPUT "Enter second Number:"; 5 While grade counter is less than or equal to ten
9 INPUT n2 6 Input the next grade
10 7 Add the grade into the total
11lcm = (n1 > n2) ? n1 : n2; 8
12 9 Set the class average to the total divided by ten
13WHILE (true) THEN 10Print the class average
14
15 IF (lcm % n1 == 0 && lcm % n2 == 0) THEN This pseudocode represents a program that calculates
16 OUTPUT "The LCM of "+n1+"and "+n2+" is "+ the average of ten grades. The program starts by
17lcm; initializing two variables: total and grade
18 BREAK WHILE; counter. Total is set to zero and grade counter is set to
19 END IF one.
20 The program then enters a WHILE loop that continues
21 ++lcm; as long as grade counter is less than or equal to ten. The
22 loop contains two statements: an INPUT statement that
23END WHILE inputs the next grade, and an ADD statement that adds
24 the input grade to the running total. After each iteration
25END of the loop, the value of grade counter is incremented
by one.
This is a pseudocode program that calculates the least Once the loop has completed, the program calculates
common multiple (LCM) of two numbers, n1 and n2, the class average by dividing the total of all grades by
which are input by the user. The program starts by ten. Finally, the program prints the class average by
initializing variables n1, n2, and lcm. Then, it prompts using the PRINT statement.
the user to enter the two numbers. Next, it sets the This pseudocode provides a clear and concise
value of lcm to the larger of n1 and n2. The program representation of the program’s logic, making it easy to
then enters an infinite loop. Within the loop, it checks if understand and implement. The use of a WHILE loop
the current value of lcm is evenly divisible by both n1 allows the program to handle an arbitrary number of
and n2. If this condition is met, the program outputs the grades, and the clear and concise statements make the
result by concatenating the values of n1, n2, and lcm in program’s logic easy to follow.
a string and outputting the string. Finally, it uses the Calculating Average of User-Entered Grades with
“break” statement to exit the loop. If the condition is not Sentinel Value Pseudocode
met, the program increments the value of lcm and 1
continues the loop. 2 Initialize total to zero
Pass/Fail Grade Checker 3 Initialize counter to zero
1 4 Input the first grade
2If student's grade is greater than or equal to 60 5
6 while the user has not as yet entered the sentinel 19
7 add this grade into the running total This pseudocode represents a program that tracks the
8 add one to the grade counter results of ten exams and outputs the number of passed
9 input the next grade (possibly the sentinel) and failed exams. The program starts by initializing
10 three variables: passes, failures, and student
11if the counter is not equal to zero counter. Passes and failures are set to zero, and student
12 set the average to the total divided by the counter counter is set to one.
13 print the average The program then enters a WHILE loop that continues
14else as long as the student counter is less than or equal to
15 print 'no grades were entered' ten. The loop contains three statements:
16 an INPUT statement that inputs the result of the next
This pseudocode represents a program that calculates exam, an IF statement that increments the number
the average of a set of grades entered by a user, using a of passes by one if the student passed the exam, and
sentinel value to signal the end of the data. The an ELSE statement that increments the number
program starts by initializing two of failures by one if the student failed the exam. The
variables: total and counter. Total is set to zero loop also contains an ADD statement that increments
and counter is set to zero. the student counter by one.
The program then inputs the first grade and enters Once the loop has completed, the program outputs the
a WHILE loop that continues as long as the user has not number of passes and failures, and then checks if eight
entered the sentinel value. The loop contains three or more students passed. If eight or more students
statements: an ADD statement that adds the input passed, the program outputs the message “raise
grade to the running total, an ADD statement that tuition”.
increments the grade counter by one, and This pseudocode provides a clear and concise
an INPUT statement that inputs the next grade representation of the program’s logic, making it easy to
(possibly the sentinel). understand and implement. The use of a WHILE loop
Once the loop has completed, the program checks if allows the program to handle an arbitrary number of
the counter is not equal to zero. If it is not, the program students and exams, and the clear and concise
calculates the average by dividing the total of all grades statements make the program’s logic easy to follow. The
by the number of grades, and then prints the average. If program also implements a simple check for whether
the counter is equal to zero, the program prints the the school should raise tuition based on the number of
message “no grades were entered”. passed exams.
This pseudocode provides a clear and concise Pseudocode of Bubble sort technique
representation of the program’s logic, making it easy to 1
understand and implement. The use of a WHILE loop 2 Set n to number of records to be sorted
allows the program to handle an arbitrary number of 3
grades, and the clear and concise statements make the 4 repeat
program’s logic easy to follow. The use of a sentinel 5 flag = false;
value ensures that the program can continue processing 6
grades until the user signals that they are done, making 7 for counter = 1 to n-1 do
the program more flexible and user-friendly. 8 if key[counter] > key[counter+1] then
Additionally, the program handles the case where no 9 swap the records;
grades were entered by checking the value of 10 set flag = true;
the counter variable and printing an appropriate 11 end if
message. 12 end do
Student passed and failured in pseudocode 13
1 14 n = n-1;
2 initialize passes to zero 15until flag = false or n=1
3 initialize failures to zero 16
4 initialize student to one This pseudocode represents a program that implements
5 a sorting algorithm known as the “Bubble Sort”
6 while student counter is less than or equal to ten method. The program sorts an array of records
7 input the next exam result represented by the key variable.
8 if the student passed The program starts by setting the n variable to the
9 add one to passes number of records to be sorted. The program then
10 else enters a REPEAT loop that continues until
11 add one to failures the flag variable is set to false or n is equal to one.
12 add one to student counter The REPEAT loop contains a nested FOR loop that
13 iterates over the array of records. The FOR loop starts
14print the number of passes with counter equal to one and continues until counter is
15print the number of failures less than n-1. The FOR loop contains an IF statement
16 that checks whether the current record (key[counter]) is
17if eight or more students passed greater than the next record (key[counter+1]). If this
18 print "raise tuition"
condition is true, the records are swapped using The program continues to repeat the REPEAT loop until
the swap statement, and the flag variable is set to true. the flag variable is not set, indicating that all the keys
At the end of each iteration of the REPEAT loop, have been sorted into the correct order.
the n variable is decremented by one. This means that This pseudocode provides a clear and concise
the next iteration of the REPEAT loop will consider one representation of the Bubble Sort algorithm, making it
fewer record, since the largest record will have easy to understand and implement. The use of
“bubbled up” to the end of the array. a REPEAT loop and a FOR loop allow the program to
This pseudocode provides a clear and concise sort an arbitrary number of keys, and the clear and
representation of the Bubble Sort algorithm, making it concise statements make the program’s logic easy to
easy to understand and implement. The use of follow.
a REPEAT loop and a FOR loop allow the program to Pseudocode Example: Defining a Function with FOR
sort an arbitrary number of records, and the clear and Loop and IF Statement
concise statements make the program’s logic easy to 1
follow. 2 FUNCTION name_of_function (input_parameters)
OR the same can be expressed more concisely in 3 DECLARE local_variables
words as below 4 BEGIN function_body
1 5 FOR i = 1 TO n
2 repeat 6 IF condition THEN
3 set a flag to False 7 statements
4 for each pair of keys 8 ELSE
5 if the keys are in the wrong order then 9 statements
6 swap the keys 10 END IF
7 set the flag to True 11 NEXT i
8 end if 12 statements
9 next pair 13 END function_body
10until flag is not set. 14END FUNCTION
11 15
This pseudocode represents a program that implements Explanation:
a sorting algorithm known as the “Bubble Sort” The structure of the function is similar to the
method. The program sorts an array of keys. previous example.
The program starts with a REPEAT loop that continues The FOR loop is used to repeat the statements within
until the flag variable is not set. The REPEAT loop it n times. i is the loop variable that starts at 1 and
contains a FOR loop that iterates over each pair of keys goes up to n.
in the array. The FOR loop contains an IF statement that The IF statement is used to perform a different set of
checks whether the current pair of keys are in the statements depending on the value of condition.
wrong order. If this condition is true, the keys are If condition is true, the statements in
swapped using the swap statement, and the THEN clause are executed. If condition is false,
the flag variable is set to True. the statements in the ELSE clause are executed.
QuickSort Algorithm using Function Declaration
1
2 FUNCTION QuickSort(array, startIndex, endIndex)
3 DECLARE pivotIndex
4 IF startIndex < endIndex THEN
5 pivotIndex = Partition(array, startIndex,
6 endIndex)
7 QuickSort(array, startIndex, pivotIndex - 1)
8 QuickSort(array, pivotIndex + 1, endIndex)
9 END IF
10END FUNCTION
11
12FUNCTION Partition(array, startIndex, endIndex)
13 DECLARE pivotValue, storeIndex
14 pivotValue = array[endIndex]
15 storeIndex = startIndex
16 FOR i = startIndex TO endIndex - 1
17 IF array[i] <= pivotValue THEN
18 SWAP array[i], array[storeIndex]
19 storeIndex = storeIndex + 1
20 END IF
21 NEXT i
22
23 SWAP array[storeIndex], array[endIndex]
24
25 RETURN storeIndex
26
27END FUNCTION
1. FUNCTION QuickSort(array, startIndex, endIndex): declares the start of the function QuickSort which takes an
array, the starting index and the ending index of the portion of the array that needs to be sorted.
2. DECLARE pivotIndex: declares a variable pivotIndex which will be used to store the pivot index.
3. IF startIndex < endIndex THEN: checks if the starting index is less than the ending index, if it is true then the
function continues to execute.
4. pivotIndex = Partition(array, startIndex, endIndex): assigns the value returned by the function Partition to
the pivotIndex variable. The Partition function is called with the array, startIndex and endIndex as input
parameters.
5. QuickSort(array, startIndex, pivotIndex - 1): calls the QuickSort function with the portion of
the array from startIndex to pivotIndex - 1 as input parameters.
6. QuickSort(array, pivotIndex + 1, endIndex): calls the QuickSort function with the portion of
the array from pivotIndex + 1 to endIndex as input parameters.
7. END IF: marks the end of the IF statement.
8. END FUNCTION: marks the end of the QuickSort function.
9. FUNCTION Partition(array, startIndex, endIndex): declares the start of the function Partition which takes an array,
the startIndex and the endIndex as input parameters.
10. DECLARE pivotValue, storeIndex: declares two variables pivotValue and storeIndex which will be used in the
function.
11. pivotValue = array[endIndex]: assigns the value at the endIndex of the array to the pivotValue variable.
12. storeIndex = startIndex: assigns the value of startIndex to the storeIndex variable.
13. FOR i = startIndex TO endIndex - 1: starts a FOR loop to iterate from startIndex to endIndex - 1.
14. IF array[i] <= pivotValue THEN: checks if the value of the current iteration is less than or equal to the pivotValue.
15. SWAP array[i], array[storeIndex]: if the above condition is true, then the value at the ith index of the array is
swapped with the value at the storeIndex of the array.
16. storeIndex = storeIndex + 1: the value of storeIndex is incremented by 1.
17. NEXT i: marks the end of the FOR loop.
18. SWAP array[storeIndex], array[endIndex]: swaps the value at the storeIndex of the array with the value at
the endIndex of the array.
19. RETURN storeIndex: returns the value of storeIndex from the Partition function.
20. END FUNCTION: marks the end of the Partition function.