What is Pseudocode? Pseudocode Examples

Posted in

What is Pseudocode? Pseudocode Examples
vinaykhatri

Vinay Khatri
Last updated on November 24, 2024

    If you are new to programming and starting your journey by learning a new programming language. You come across the term pseudocode and wonder what pseudocode is and why to learn and use it?

    This article will discuss what pseudocode is in computer science and programming and why to use it. For a better understanding, we will practice writing pseudocode with the help of some examples.

    What is Pseudocode

    As the name suggests, Pseudocode means fake code or false code. It is an informal way to write algorithms for a problem statement. It is not a programming language which means it can not be compiled or interpreted by the computer. It is just fake code developers use to put the wireframe or roughly algorithm for any problem. But Pseudocode is not a programming language, which means it does not have fixed syntax.

    Programming languages like C++, Python, Java, JavaScript, kotlin, etc., have a strict syntax, and when we have to follow that syntax if we want to write programs in these programming languages. The only reason why we write pseudocode is so any programmer could understand the working and implementation of an algorithm irrespective of the Programming language he/she knows.

    Mostly, all the programming languages share similar structures, statements, and operators. Pseudocode helps to bring all those common statements together to write the plain English code-like instruction for an algorithm. You can say that pseudocode is just a blueprint for any algorithm, and ultimately that algorithm needs to be implemented in a programming language.

    Application of Pseudocode

    Here are some significant pseudocode applications

    1. Pseudocode is used by textbooks, programming languages documentation, and numerical computation publication to define the algorithm working.
    2. In Programming Interviews, the Interviewer often asked developers to write the pseudocode for the problem statement rather than a specific programming language.
    3. Pseudocode is often used to implement a specific algorithm before the developers start the project and implement the algorithm in the targeted programming language.
    4. By writing the pseudocode, you can increase your logical skills. Because pseudocode is all about writing the algorithm from scratch without depending upon the specific programming language inbuilt function.

    How to write the Pseudocode?

    As discussed in the “What is Pseudocode section” section, Pseudocode does not have a rigid syntax because it is not a programming language. Sometimes writers and programmers borrow the syntax from the target programming languages and sometimes they just write rough instructions from top to bottom.

    Despite the different syntax followed by the different developers, the main objective of writing the pseudocode is that every developer could understand the written algorithm. Here are some conventional rules for writing pseudocode that many developers follow.

    1. Use a single line to write a single statement.
    2. Use all uppercase letters for the keywords such as IF, ELSE, FOR, WHILE, CLASS, FUNCTION, etc.
    3. Use lowercase and camel casing for variable or identifier names.
    4. Try to avoid the target language's inbuilt functions.
    5. Use the proper indentation for the block code.
    6. Make the instructions as simple as possible.

    Pseudocode Examples

    Now let’s write some algorithms using pseudocode.

    Pseudocode Example 1:Write a Pseudocode to add two numbers

    INTEGER: number1, number2, total
    
    total = number1 +number2
    
    PRINT "The total of", number1, number2, "is", total

    Pseudocode Example 2:Write a Pseudocode to find the largest number between two numbers.

    NUMBER: number1, number2, largest
    
    number1 = INPUT: "Input the value for number1"
    number2 = INPUT: "Input the value for number2"
    
    BEGIN:
    
        IF: number1 >= number2:
            largest = number1
        ELSE:
            largest = number2
    
    END
    
    PRINT "The largest number is", largest

    Pseudocode Example 3:Write a Pseudocode to calculate the sum of three numbers.

    NUMBER: number1, number2, number3, total
    
    number1 = INPUT: "Input the value for number1"
    number2 = INPUT: "Input the value for number2"
    number3 = INPUT: "Input the value for number3"
    
    total = number1 + number2 + number3
    
    PRINT "The sum of", number1, number2, number3, "is", total

    Pseudocode Example 4:Write a Pseudocode to calculate the area of a rectangle.

    NUMBER: length, width, area
    
    length = INPUT: "Input the rectangle length"
    width = INPUT: "Input the rectangle width"
    
    area =  width * length
    
    PRINT "The area of the rectangle is", area

    Pseudocode Example 5:Write a Pseudocode to calculate the perimeter of a rectangle.

    NUMBER: length, width, perimeter
    
    length = INPUT: "Input the rectangle length"
    width = INPUT: "Input the rectangle width"
    
    perimeter = 2 * (width + length)
    
    PRINT "The perimeter of the rectangle is", perimeter

    Pseudocode Example 6:Write a Pseudocode to calculate the area of a square.

    NUMBER: length, area
    
    length = INPUT: "Input the square length"
    
    area = length * length
    
    PRINT "The area  of the square is", area

    Pseudocode Example 7:Write a Pseudocode to calculate the perimeter of a square.

    NUMBER: length, perimeter
    
    length = INPUT: "Input the square length"
    
    perimeter = 4 * length
    
    PRINT "The perimeter  of the square is", perimeter

    Pseudocode Example 8:Write a Pseudocode to calculate the area of a circle.

    NUMBER: radius, area
    
    radius = INPUT: "Input the circle radius"
    
    area = radius * radius * 3.14
    
    PRINT "The area of the circle is", area

    Pseudocode Example 9:Write a Pseudocode to calculate the circumference of a circle.

    NUMBER: radius, circumference
    
    radius = INPUT: "Input the circle radius"
    
    circumference =  2 * 3.14 * radius
    
    PRINT "The circumference of the circle is", circumference

    Pseudocode Example 10:Write a Pseudocode to find the greatest number among three numbers.

    NUMBER: number1, number2, number3, largest
    
    number1 : INPUT: "Enter the value of number1"
    number2 : INPUT: "Enter the value of number2"
    number3 : INPUT: "Enter the value of number3"
    
    BEGIN
    IF number1 > number2 AND number1 > number3:
        largest = number1
    
    ELSE IF number2 > number1 AND number2 > number3:
        largest = number2
    
    ELSE:
        largest = number3
    
    END
    
    PRINT : "Among", number1, number2, number3, "the largest number is", largest

    Pseudocode Example 11:Write a Pseudocode to find if the entered number is prime or odd

    NUMBER: number
    
    number = INPUT "Enter the number"
    
    BEGIN
    
    IF number%2==0:
        PRINT: "The number is even"
    ELSE:
        PRINT: "The number is odd"
    
    END

    Pseudocode Example 12:Write a Pseudocode to find if the entered numbers are equal.

    NUMBER: number1, number2
    
    number1 = INPUT "Enter the number1"
    number2 = INPUT "Enter the number2"
    
    BEGIN
    
    IF number1 == number2:
        PRINT: "Both the numbers are equal"
    ELSE:
        PRINT: "Numbers are not equal"
    
    END

    Pseudocode Example 13:Write a Pseudocode to find the smallest number among three numbers.

    NUMBER: number1, number2, number3, smallest
    
    
    number1 : INPUT: "Enter the value of number1"
    number2 : INPUT: "Enter the value of number2"
    number3 : INPUT: "Enter the value of number3"
     
    BEGIN
    
    IF number1 < number2 AND number1 < number3:
        smallest = number1
    
    ELSE IF number2 < number1 AND number2 < number3:
        smallest = number2
    
    ELSE:
        smallest = number3
    END
    
    PRINT : "Among", number1, number2, number3, "the smallest number is", smallest

    Pseudocode Example 14:Write a Pseudocode to find if the entered year is a leap year or not

    INTEGER: year
    
    year = INPUT: "Enter the year"
    
    BEGIN
    
    IF year % 4 == 0:
        IF year % 100 == 0:
            IF year % 400 == 0:
                PRINT : "It's a Leap year"
            ELSE:
                PRINT :  "It's not a Leap year"
        ELSE:
            PRINT : "It's a leap year"
    ELSE:
        PRINT : "It's not a leap year"
    
    END

    Pseudocode Example 15:Write a Pseudocode to display the names of the day based on the user entered number

    INTEGER: day
    
    day = INPUT: "Enter the day 1 t0 7"
    
    BEGIN
    
    IF day == 1:
        PRINT: "It's MONDAY"
    ELSE iF day ==2:
        PRINT: "It's TUESDAY"
    ELSE iF day ==3:
        PRINT: "It's WEDNESDAY"
    ELSE iF day ==4:
        PRINT: "It's THURSDAY"
    ELSE iF day ==5:
        PRINT: "It's FRIDAY"
    ELSE iF day ==6:
        PRINT: "It's SATURDAY"
    ELSE iF day ==7:
        PRINT: "It's SUNDAY"
    ELSE:
        PRINT: "Invalid Day"
    
    END

    Pseudocode Example 16:Write a Pseudocode to display the names of the month based on the user entered number

    INTEGER: month
    
    month = INPUT: "Enter the day 1 t0 12"
    
    BEGIN
    
    IF day == 1:
        PRINT: "It's JANUARY"
    ELSE iF day ==2:
        PRINT: "It's FEBRUARY"
    ELSE iF day ==3:
        PRINT: "It's MARCH"
    ELSE iF day ==4:
        PRINT: "It's APRIL"
    ELSE iF day ==5:
        PRINT: "It's MAY"
    ELSE iF day ==6:
        PRINT: "It's JUNE"
    ELSE iF day ==7:
        PRINT: "It's JULY"
    ELSE iF day ==8:
        PRINT: "It's AUGUST"
    ELSE iF day =9:
        PRINT: "It's SEPTEMBER"
    ELSE iF day ==10:
        PRINT: "It's OCTOBER"
    ELSE iF day =11:
        PRINT: "It's NOVEMBER"
    ELSE iF day ==12:
        PRINT: "It's DECEMBER"
    ELSE :
        PRINT: "Month OUT of range"
    
    END

    Pseudocode Example 17:Write a Pseudocode to display the even numbers between 1 to 100

    BEGIN
    
    INTEGER i
    
    FOR i -> 1 to 100:
        IF i % 2 == 0:
            PRINT i
    
    END

    Pseudocode Example 18:Write a Pseudocode to display all the odd numbers from 0 upto n

    BEGIN
    
    INTEGER i, n
    n = INPUT: "Enter the value of n"
    
    FOR i -> 1 to n:
        IF NOT i % 2 == 0:
            PRINT i
    END

    Pseudocode Example 19:Write a Pseudocode to calculate the sum of series 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8.. n

    INTEGER: total, n
    
    n = INPUT: "Enter the value of n for 1+2+3+4+5...n"
    
    total = (n * (1 + n) ) / 2
    
    PRINT: "The sum of first n numbers is", total

    Pseudocode Example 20:Write a Pseudocode to calculate the sum of series 1 + 3 + 5 + 7 ..n

    INTEGER: total, n, i
    
    n = INPUT: "Enter the value of n for 1+3+5...n"
    
    BEGIN
    
    FOR i-> 1 to n;  i+=2:
        total += i
    
    END
    PRINT: "The sum of 1+3+5...n series is", total

    Conclusion

    Before directly implementing the algorithm in the targeted programming language , it’s always good to write its pseudocode for all the shortcomings, discussion, and complexities.

    In this article, we discussed what pseudocode is and its applications. We also see some pseudocode examples that implement basic algorithms. Learning how to write pseudocode is not a mandatory skill for a developer, it’s just an extra skill that comes in very handy to break down the code using pen and paper.

    People are also reading:

    FAQs


    No, pseudocode does not have any syntax. Instead, it is written in information text and annotations in simple English language.

    An algorithm is a step-by-step, well-defined procedure that helps programmers to write code. Meanwhile, pseudocode is more similar to programming code written in plain English using short phrases.

    There are two reasons to write pseudocode before implementing code in a specific programming language: 1. People from non-programming backgrounds can easily understand the code 2. It is an environment and programming language-independent description of an algorithm.

    Flowcharts are the diagrammatic representation of an algorithm. As an algorithm, a flowchart is also a program-planning tool to solve a specific problem.

    A flowchart is ideal for small and simple problems, while pseudocode is ideal for large and complex problems.

    Leave a Comment on this Post

    0 Comments