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