How To Write A Pseudocode

Last Updated on January 19, 2023

Right here on Collegelearners, you are privy to a litany of relevant information on pseudocode keywords list, pseudocode generator, pseudocode examples for beginners, and so much more. Take out time to visit our catalog for more information on similar topics.

How To Write A Pseudocode

Pseudo code is a term which is often used in programming and algorithm based fields. It is a methodology that allows the programmer to represent the implementation of an algorithm. Simply, we can say that it’s the cooked up representation of an algorithm. Often at times, algorithms are represented with the help of pseudo codes as they can be interpreted by programmers no matter what their programming background or knowledge is. Pseudo code, as the name suggests, is a false code or a representation of code which can be understood by even a layman with some school level programming knowledge.

Algorithm: It’s an organized logical sequence of the actions or the approach towards a particular problem. A programmer implements an algorithm to solve a problem. Algorithms are expressed using natural verbal but somewhat technical annotations.

Pseudo code: It’s simply an implementation of an algorithm in the form of annotations and informative text written in plain English. It has no syntax like any of the programming language and thus can’t be compiled or interpreted by the computer.

Advantages of Pseudocode

  • Improves the readability of any approach. It’s one of the best approaches to start implementation of an algorithm.
  • Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough documentation, so the program of one developer can be understood easily when a pseudo code is written out. In industries, the approach of documentation is essential. And that’s where a pseudo-code proves vital.
  • The main goal of a pseudo code is to explain what exactly each line of a program should do, hence making the code construction phase easier for the programmer.

How to write a Pseudo-code?

  1. Arrange the sequence of tasks and write the pseudocode accordingly.
  2. Start with the statement of a pseudo code which establishes the main goal or the aim.Example:This program will allow the user to check the number whether it’s even or odd.
  3. The way the if-else, for, while loops are indented in a program, indent the statements likewise, as it helps to comprehend the decision control and execution mechanism. They also improve the readability to a great extent.Example: if “1” print response “I am case 1” if “2” print response “I am case 2”
  4. Use appropriate naming conventions. The human tendency follows the approach to follow what we see. If a programmer goes through a pseudo code, his approach will be the same as per it, so the naming must be simple and distinct.
  5. Use appropriate sentence casings, such as CamelCase for methods, upper case for constants and lower case for variables.
  6. Elaborate everything which is going to happen in the actual code. Don’t make the pseudo code abstract.
  7. Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’ the way we use it in programming.
  8. Check whether all the sections of a pseudo code is complete, finite and clear to understand and comprehend.
  9. Don’t write the pseudo code in a complete programmatic manner. It is necessary to be simple to understand even for a layman or client, hence don’t incorporate too many technical terms. Dos and Don'ts in  Pseudo Code Writing

Example:

Let’s have a look at this code

  • Java
// This program calculates the Lowest Common multiple// for excessively long input values  import java.util.*;  public class LowestCommonMultiple {      private static long    lcmNaive(long numberOne, long numberTwo)    {          long lowestCommonMultiple;          lowestCommonMultiple            = (numberOne * numberTwo)              / greatestCommonDivisor(numberOne,                                      numberTwo);          return lowestCommonMultiple;    }      private static long    greatestCommonDivisor(long numberOne, long numberTwo)    {          if (numberTwo == 0)            return numberOne;          return greatestCommonDivisor(numberTwo,                                     numberOne % numberTwo);    }    public static void main(String args[])    {          Scanner scanner = new Scanner(System.in);        System.out.println("Enter the inputs");        long numberOne = scanner.nextInt();        long numberTwo = scanner.nextInt();          System.out.println(lcmNaive(numberOne, numberTwo));    }}

And here’s the Pseudo Code for the same.

This program calculates the Lowest Common multiple for excessively long input values  function lcmNaive(Argument one, Argument two){      Calculate the lowest common variable of Argument    1 and Argument 2 by dividing their product by their    Greatest common divisor product      return lowest common multipleend}function greatestCommonDivisor(Argument one, Argument two){    if Argument two is equal to zero        then return Argument one      return the greatest common divisor  end}  {In the main function        print prompt "Input two numbers"            Take the first number from the user   Take the second number from the user     Send the first number and second number    to the lcmNaive function and print   the result to the user   }

pseudocode examples for beginners

What is Pseudocode

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

Some Keywords That Should be Used

For looping and selection, The keywords that are to be used include Do While…EndDo; Do Until…Enddo; Case…EndCase; If…Endif; Call … with (parameters); Call; Return ….; Return; When; Always use scope terminators for loops and iteration.As verbs, use the words Generate, Compute, Process, etc. Words such as set, reset, increment, compute, calculate, add, sum, multiply, … print, display, input, output, edit, test , etc. with careful indentation tend to foster desirable pseudocode.

Do not include data declarations in your pseudocode.

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.

Pseudocode Example 1: Add Two Numbers.(Simple Pseudocode  Example)INI

1234567891011 BEGINNUMBER s1, s2, sumOUTPUT(“Input number1:”)INPUT s1OUTPUT(“Input number2:”) INPUT s2sum=s1+s2OUTPUT sumEND 

Flowchart of Pseudocode

flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 2: Calculate Area and Perimeter of Rectangle (Simple Pseudocode  Example)INI

1234567891011 BEGINNUMBER b1,b2,area,perimeterINPUT b1UNPUT b2area=b1*b2perimeter=2*(b1+b2)OUTPUT alanOUTPUT perimeterEND 

Flowchart of Pseudocode

flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 3: Find Area and Perimeter of a Square (Simple Pseudocode Example)INI

12345678910 BEGINNUMBER len, area,perimeterINPUT lenarea = len*lenperimeter = len*4OUTPUT areaOUTPUT perimeterEND 

Pseudocode Example 4: Find Area Of Circle using Radius (Simple Pseudocode  Example)INI

12345678 BEGINNUMBER r, area INPUT r area=3.14*r*r OUTPUT area END 

Flowchart of Pseudocode

Pseudocode Example 5: Find Perimeter Of Circle using Radius (Simple Pseudocode  Example)INI

12345678 BEGINNUMBER r, perimeter INPUT r perimeter=2*3.14*rOUTPUT perimeterEND 

Flowchart of Pseudocode

Pseudocode Example 6: Calculate sales taxes (Simple Pseudocode  Example)INI

1234567891011121314151617 BEGINNUMBER price, tax, taxRate, total OUTPUT “Enter Product Price”INPUT priceOUTPUT “Enter tax rate amoung 1 and 100” OKU taxRate tax= price* taxRate/100total= price + tax OUTPUT “Product tax=”+taxOUTPUT “Product total price =”+total END 

Flowchart of Pseudocode

flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 7:  Solve Quadratic Equation (Pseudocode If Else Example)INI

12345678910111213141516171819 BEGINNUMBER a, b, c, d, x1, x2INPUT a,b,cd = b^2-4acIF (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:”+x2ELSE    OUTPUT “There is no real root”ENDIF END 

Flowchart of Pseudocode

flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 8: issue for driver licence (Pseudocode If Else Example)

123456789101112131415 BEGINNUMBER 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 

Flowchart of Pseudocode

flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 9: Check a Number is Positive or Negative (Pseudocode If Else Example)INI

123456789101112131415161718 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 

Flowchart of Pseudocode

flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 10: Find the biggest of three (3) Numbers (Pseudocode If Else Example)INI

12345678910111213141516171819 BEGIN NUMBER num1,num2,num3 INPUT num1INPUT num2INPUT 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 

Flowchart of Pseudocode

flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 11: Print Numbers from 1 to 100. (Pseudocode For Loop Example)

12345678910 BEGIN NUMBER counter FOR  counter = 1 TO  100 STEP 1 DO     OUTPUT counterENDFOR END 

Flowchart of Pseudocode

Pseudocode Example 12: Find Sum of Natural Numbers (1 to 100). (Pseudocode For Loop Example)INI

123456789101112 BEGIN NUMBER counter, sum=0 FOR counter=1 TO 100 STEP 1 DO     sum=sum+counterENDFOROUTPUT sum END 

Pseudocode Example 13: Read 50 numbers and find their sum and average. (Pseudocode For Loop Example)INI

123456789101112131415 BEGIN NUMBER counter, sum=0, num FOR counter=1 TO 50 STEP counter DO     OUTPUT “Enter a Number”     INPUT num     sum=sum+numENDFOR OUTPUT sum END 

Flowchart of Pseudocode

flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 14: Read 10 numbers and find sum of even numbers. (Pseudocode For Loop Example)INI

1234567891011121314151617 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     ENDIFENDFOROUTPUT sum END 

Flowchart of Pseudocode

Pseudocode Example 15: Find the sum of all elements of array. (Pseudocode For Loop Example)INI

123456789101112 BEGINNUMBER i=0, n=5, sum=0ARRAY 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 

Flowchart of Pseudocode

Pseudocode Example 16: Calculate square of a number (Simple Pseudocode  Example)

Alternative 1:INI

123456789101112 BEGIN NUMBER num, resultOUTPUT “Enter a number for calculate the power of the number”INPUT num result=num*num OUTPUT “Power of the Number:” + result  END 

Alternative 2:INI

123456789101112131415 BEGINNUMBER 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 +=numENDFOR OUTPUT “Power of the Number:” + result  END 

Pseudocode Example 17: Calculate the Square Root of a Number (Pseudocode For Loop Example)INI

1234567891011121314151617 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)/2END WHILE OUTPUT root END 
flowchart examples, flowchart examples for student, flowchart examples for programming

Pseudocode Example 18: Swap two variables with using a temporary variable (Simple Pseudocode  Example)INI

12345678910111213141516 BEGINNUMBER a,b,ca=10, b=20OUTPUT “Value of a :”+aOUTPUT “Value of b :”+b c=aa=bb=c OUTPUT “Value of a :”+a OUTPUT “Value of b :”+b END 

Pseudocode Example 19: Swap two variables without using a temporary variable (Simple Pseudocode  Example)INI

12345678910111213141516 BEGINNUMBER a,ba=10, b=20OUTPUT “Value of a :”+a OUTPUT “Value of b :”+b a=a+bb=a-ba=a-b OUTPUT “Value of a :”+a OUTPUT “Value of b :”+b END 

Pseudocode Example 20: Print Numbers from 1 to n. (Pseudocode For Loop Example)

12345678910 BEGIN NUMBER counter,nINPUT nFOR  counter = 1 TO  n STEP 1 DO     OUTPUT counterENDFOR END 

Pseudocode Example 21: Calculate the Sum and Average of n Number. (Pseudocode For Loop Example)

123456789101112 BEGIN NUMBER counter,n,sum,avgINPUT nFOR  counter = 1 TO  n STEP 1 DO     sum=sum+counterENDFORavg=sum/nOUTPUT “Sum of numbers :”+sumOUTPUT “Average of numbers :”+avgEND 

Pseudocode Example 22: Design the algorithm and flowchart that finds and display the larger of the two numbers given different from each other.

123456789101112131415161718 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 IFEND 

Pseudocode Example 23: Perform the application that calculates the area of the triangle whose height and base length entered by the keyboard.

12345678910111213 BEGIN Number b,h,areaInput b Input h area=(b*h)/2OUTPUT (Area of Triangle is “+area)END 

Pseudocode Example 24: 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.

123456789 BEGINNumber i,r,vInput iInput rv=i*rOUTPUT (v)END 

Pseudocode Example 25: What is the algorithm that the number entered by the user from the keyboard fully divided if it is divided into 3 and 5 .

12345678 BEGINNumber numInput num    IF(num%3==0 AND num%5==0) OUTPUT(num+ ” is divided into 3 and 5″)

About the author

Study on Scholarship Today -- Check your eligibility for up to 100% scholarship.