 # Writing Pseudocode: Algorithms and Examples

An algorithm, as stated earlier, is a series of instructions written in any language spoken by human beings, such as English. An algorithm describes a way to perform a particular programming task. Generally, these instructions are written in steps, where each step performs some task. Let's proceed to write an algorithm for our program that displays registration information of the students in an educational institute:

1. Initialize New_Reg to 'Y'
2. Initialize No_Stud_A to 0
3. Initialize No_Stud_B to 0
4. Initialize No_Stud_C to 0
5. Input Course_Code
6. Check the Course_Code. If it is A, the add 1 to No_Stud_A; otherwise, move to Step 7
7. If Course_Code is B, then add 1 to No_Stud_B; otherwise, move to Step 8
8. If Course_Code is C, then add 1 to No_Stud_C,
9. Input New_Reg
10. If New_Reg is 'Y' then repeat steps 5 to 9; otherwise, move to Step 11
11. Print the total number of students registered in each course, that is, No_Stud_A, No_Stud_B, and No_Stud_C

Pseudocode

Pseudocode is a structured of English phrases that are used to describe the algorithms. It focuses on the logic of the algorithm and does not have any programming language specific keywords. Listing 1.3 shows the pseudocode for our program that displays registration information of the students of an educational institute:

Listing 1.3: Showing the Pseudocode for the Registration Problem

Initialize New_Reg to 'Y'
Initialize No_Stud_A to 0
Initialize No_Stud_B to 0
Initialize No_Stud_C to 0
While (Value of New_Reg is 'Y')
{
Course_Code = Get the coursecode from the user
If Course_Code = 'A' then
No_Stud_A = No_Stud_A + 1
If Course_Code = 'B' then
No_Stud_B = No_Stud_B + 1
If Course_Code = 'C' then
No_Stud_C = No_Stud_C + 1
New_Reg = Get the character from the user
}
Print No_Stud_A
Print No_Stud_B
Print No_Stud_C

In Listing 1.3, the variable, New_Reg is initialized, to 'Y' and each of the variables, No_Stud_A, No_Stud_B, and No_Stud_C, to 0. Then a While loop is used, which is executed repeatedly till the value of New_Reg is 'Y'. As New_Reg is initialized to 'Y' in the beginning; therefore, the While loop will be executed at least once. Inside the While loop, course code is taken as input from the user and depending upon the course code entered, the number of students registered for the respective course is increased by one. Then,a character specifying whether or not more students need to be registered is taken as input. Finally, the number of students registered for each course is printed on the screen

Pseudocode is a high-level representation of an algorithm's steps, written in a natural language-like syntax that can be easily understood by humans, but not necessarily executed by computers. It is used as a tool to describe and plan the logic of a program before writing the actual code. Pseudocode is useful for conveying the logic of an algorithm without getting bogged down in the details of a specific programming language.

Example of a simple pseudocode algorithm to calculate the factorial of a number:

``````Algorithm: Calculate Factorial
Input: An integer number n
Output: The factorial of n

1. Set result = 1
2. Repeat the following steps for i = 1 to n:
a. Multiply result by i
3. Return result
``````

Example of a pseudocode algorithm to find the largest number in a list of numbers:

``````Algorithm: Find the Largest Number
Input: A list of numbers, numbers[]
Output: The largest number in the list

1. Set largest = numbers
2. Repeat the following steps for i = 1 to the end of the list:
a. If numbers[i] is greater than largest, set largest = numbers[i]
3. Return largest
``````

Note: The syntax and structure of pseudocode may vary slightly depending on the author or the context in which it is being used. The goal is to provide a clear, concise, and human-readable representation of an algorithm's logic.

#### Related information #### Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.