Pseudocode is a compact and informal high-level description of a program using the conventions of a programming language, but intended more for humans.
Pseudocode is not an actual programming language. So it cannot be compiled into an executable program. It uses short terms or simple English language syntaxes to write code for programs before it is actually converted into a specific programming language.
And there is no pseudocode standard syntax and so at times it becomes slightly confusing when writing Pseudocode and so let us understand pseudo code with an example.
- INPUT – indicates a user will be inputting something
- OUTPUT – indicates that an output will appear on the screen
- WHILE – a loop (iteration that has a condition at the beginning)
- FOR – a counting loop (iteration)
- REPEAT – UNTIL – a loop (iteration) that has a condition at the end
- IF – THEN – ELSE – a decision (selection) in which a choice is made
- any instructions that occur inside a selection or iteration are usually indented
Example 1:
“Hello World!” A simple program that displays. It is often used to show the syntax of the language.
In this pseudocode, we have used the “OUTPUT” word to print the string Hello, world!
on our screen.
1 2 3 4 5 |
BEGIN OUTPUT "HELLO WORLD!" END |
Example 2: Pseudocode to add two numbers.
In this example, the user is asked to enter two integers. Then, the sum of these two integers is calculated and displayed on the screen.
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 |
Example 3: Pseudocode to average of two numbers.
In this example, the user is asked to enter two integers. Then, the average of these two integers is calculated and displayed on the screen.
1 2 3 4 5 6 7 8 9 10 11 |
BEGIN NUMBER s1, s2, result OUTPUT("Input number1:") INPUT s1 OUTPUT("Input number2:") INPUT s2 result=s1+s2 OUTPUT result END |
Example 4: Pseudocode to calculate area and perimeter of rectangle.
Pseudocode is a kind of structured english for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. But in this post we will use the C style syntax.
1 2 3 4 5 6 7 8 9 |
int b1,b2,area,perimeter; read b1; read b2; area=b1*b2; perimeter=2*(b1+b2); print area; print perimeter; |
Example 5: Pseudocode to calculate the area of square.
1 2 3 4 5 6 7 8 9 |
begin numeric nSide, nArea display "ENTER THE SIDE OF SQUARE : " accept nSide nArea=nSide*nSide display "AREA OF SQUARE : " nArea end |
Example 6: Pseudocode to find the greatest of two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
begin numeric nNum1, nNum2 display "ENTER THE FIRST NUMBER : " accept nNum1 display "ENTER THE SECOND NUMBER : " accept nNum2 if(nNum1>nNum2) begin display "GREATEST ONE : " nNum1 end else begin display "GREATEST ONE : " nNum2 end end |
Example 7: Pseudocode to find the greatest of three numbers.
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 28 29 30 31 |
begin numeric nNum1,nNum2,nNum3 display "ENTER THE FIRST NUMBER : " accept nNum1 display "ENTER THE SECOND NUMBER : " accept nNum2 display "ENTER THE THIRD NUMBER : " accept nNum3 if(nNum1>nNum2) begin if(nNum1>nNum3) begin display "GREATEST ONE : " nNum1 end else begin display "GREATEST ONE : " nNum3 end end else if(nNum2>nNum3) begin display "GREATEST ONE : " nNum2 end else begin display "GREATEST ONE : " nNum3 end end |
Example 8: Write pseudo code that reads two numbers and multiplies them together and print out their product.
1 2 3 4 5 |
Read num1 , num2 Set multi to num1*num2 Write multi |
Example 9: Write pseudo code that tells a user that the number they entered is not a 10 or a 20.
1 2 3 4 5 6 7 8 9 |
Read num If(num= 10) Write "your number is 10" Else if (num= 20) Write "your number is 20" Else Write "your number is not 10 or 10" |
Solution 2:
1 2 3 4 5 6 7 |
Read num If(num= 10 or num= 20) Write "your number is a 10 or 20" Else Write "your number is not 10 or 10" |
Example 10: 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.
1 2 3 4 5 6 7 8 9 10 11 12 |
Write "Please enter a number" Read colornum If (colornum >0 and colornum <= 10) Write blue else If (colornum >0 and colornum <= 10) Write blue else If (colornum >0 and colornum <= 10) Write blue else Write "not a correct color option" |