What is Pseudocode
As you know, pseudocode is the way of expressing a program or code so that it could be easily understood by programmers of every programming languages out there.
Pseudocode is an informal high-level description of the operating principle of a computer program or an algorithm
Pseudocode is a way of describing a computer program or algorithm in plain, easy-to-understand language. It is not a formal programming language, but rather a tool that programmers use to plan and organize their code.
The purpose of pseudocode is to express the logical steps of an algorithm in a way that is easy for humans to understand, while still being independent of any specific programming language. As such, pseudocode often uses a combination of natural language and programming constructs, such as loop and conditional statements, to convey the overall structure and intent of a program.
Pseudocode is often used as an intermediate step between a problem or task description, and the writing of actual code. It can be used to plan the overall structure of a program, or to work out the details of a particular algorithm or data structure.
The structure of pseudocode can be quite flexible, but it typically follows the basic format of a programming language, with variables, assignments, control flow, and subroutines. The key difference is that pseudocode doesn’t have to adhere to strict syntax rules and it can be written in natural language.
For example, the following is a simple pseudocode algorithm that finds the largest value in an array of numbers:
1 2 3 4 5 6 7 | 1. Set "maxValue" to the first value in the array. 2. For each value in the array, starting with the second value: a. If the current value is greater than "maxValue", set "maxValue" to the current value. 3. Print "maxValue". |
It’s important to note that pseudocode is not a programming language and should not be executed by a computer. Instead, it’s meant to be a helpful tool for humans to plan and understand their programs. It is also worth mentioning that there are different conventions for writing pseudocode, as it is not standardized language, so it can be written in variety of styles.
In summary, pseudocode is a useful technique for expressing algorithms and program structures in a way that is easy for humans to understand. It is often used as a tool to help programmers plan and organize their code before they begin writing it in a specific programming language.
Following are the basic rules before writing pseudocode :
- Write only one statement per line.
- Write what you mean, not how to program it
- Give proper indentation to show hierarchy and make code understandable.
- Make the program as simple as possible.
- Conditions and loops must be specified well ie. begun and ended explicity as in given pseudocode examples :
Example 1: WRITE A PSEUDOCODE TO FIND THE LARGEST 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 : " INPUT nNum1 DISPLAY "ENTER THE SECOND NUMBER : " INPUT nNum2 IF nNum1 > nNum2 DISPLAY nNum1 + " is larger than "+ nNum2 ELSE DISPLAY nNum2 + " is larger than " + nNum1 END |
The above block of text is a pseudocode that compares two numbers and prints out which one is larger. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nNum1,nNum2
declares two variables, nNum1 and nNum2, as numeric data types.DISPLAY "ENTER THE FIRST NUMBER : "
prints the string “ENTER THE FIRST NUMBER :” to the screen.INPUT nNum1
waits for the user to enter a value, which is then stored in the variable nNum1.DISPLAY "ENTER THE SECOND NUMBER : "
prints the string “ENTER THE SECOND NUMBER :” to the screen.INPUT nNum2
waits for the user to enter a value, which is then stored in the variable nNum2.IF nNum1 > nNum2
checks if the value stored in nNum1 is greater than the value stored in nNum2.- If nNum1 > nNum2 is true, then the next line is executed,
DISPLAY nNum1 + " is larger than "+ nNum2
which will print the value of nNum1 and a string that says ” is larger than ” and value of nNum2. - If the value of nNum1 is not greater than nNum2, the next line is executed:
DISPLAY nNum2 + " is larger than " + nNum1
which will print the value of nNum2 and a string that says ” is larger than ” and value of nNum1. END
is a marker that indicates the end of the program.
This pseudocode will take two numbers as input from the user, compare them and prints which number is greater. It follows the basic flow of control statements, variable declarations, and user input/output.
Example 2: WRITE A PSEUDOCODE TO FIND THE SUM OF TWO NUMBERS.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nNum1,nNum2,nSum display "ENTER THE FIRST NUMBER : " accept nNum1 display "ENTER THE SECOND NUMBER : " accept nNum2 compute nSum=nNum1+nNum2 display "SUM OF THESE NUMBER : " nSum end |
The above block of text is a pseudocode that prompts the user to enter two numbers and then calculates and displays their sum. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nNum1,nNum2,nSum
declares three variables, nNum1, nNum2 and nSum, as numeric data types.DISPLAY "ENTER THE FIRST NUMBER : "
prints the string “ENTER THE FIRST NUMBER :” to the screen.ACCEPT nNum1
waits for the user to enter a value, which is then stored in the variable nNum1.DISPLAY "ENTER THE SECOND NUMBER : "
prints the string “ENTER THE SECOND NUMBER :” to the screen.ACCEPT nNum2
waits for the user to enter a value, which is then stored in the variable nNum2.COMPUTE nSum=nNum1+nNum2
calculates the sum of the two numbers stored in nNum1 and nNum2, and assigns the result to the variable nSum.DISPLAY "SUM OF THESE NUMBER : " nSum
prints the string “SUM OF THESE NUMBER :” and the value of the nSum variable.END
is a marker that indicates the end of the program.
This pseudocode prompts the user to enter two numbers and then adds them. The sum is then stored in nSum variable and the program then prints the sum. The program follows the basic flow of input, computation and output.
Example 3: WRITE A PSEUDOCODE TO FIND THE SUM OF THREE NUMBERS.
1 2 3 4 5 6 7 8 9 10 11 12 13 | begin numeric nNum1,nNum2,nNum3,nSum display "ENTER THE FIRST NUMBER : " accept nNum1 display "ENTER THE SECOND NUMBER : " accept nNum2 display "ENTER THE THIRD NUMBER : " accept nNum3 nSum=nNum1+nNum2+nNum3 display "SUM OF ALL THREE NUMBERS : " nSum end |
The above block of text is a pseudocode that prompts the user to enter three numbers and then calculates and displays their sum. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nNum1,nNum2,nNum3,nSum
declares four variables, nNum1, nNum2, nNum3 and nSum, as numeric data types.DISPLAY "ENTER THE FIRST NUMBER : "
prints the string “ENTER THE FIRST NUMBER :” to the screen.ACCEPT nNum1
waits for the user to enter a value, which is then stored in the variable nNum1.DISPLAY "ENTER THE SECOND NUMBER : "
prints the string “ENTER THE SECOND NUMBER :” to the screen.ACCEPT nNum2
waits for the user to enter a value, which is then stored in the variable nNum2.DISPLAY "ENTER THE THIRD NUMBER : "
prints the string “ENTER THE THIRD NUMBER :” to the screen.ACCEPT nNum3
waits for the user to enter a value, which is then stored in the variable nNum3.nSum=nNum1+nNum2+nNum3
calculates the sum of all three numbers, nNum1, nNum2 and nNum3 and assigns the result to the variable nSum.DISPLAY "SUM OF ALL THREE NUMBERS : " nSum
prints the string “SUM OF ALL THREE NUMBERS :” and the value of the nSum variable.END
is a marker that indicates the end of the program.
This pseudocode prompts the user to enter three numbers and then adds them all together. The sum is then stored in nSum variable and the program then prints the sum. The program follows the basic flow of input, computation and output.
Example 4: WRITE A PSEUDOCODE TO FIND THE AREA OF RECTANGLE.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nLen,nBrd,nAre display "ENTER THE LENGTH OF RECTANGLE : " accept nLen display "ENTER THE BREADTH OF RECTANGLE : " accept nBrd nAre=nLen*nBrd display "AREA OF RECTANGLE : " nAre end |
The above block of text is a pseudocode that calculates the area of a rectangle by prompt the user to enter the length and breadth of the rectangle and then calculates and display the area. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nLen,nBrd,nAre
declares three variables, nLen, nBrd, and nAre, as numeric data types.DISPLAY "ENTER THE LENGTH OF RECTANGLE : "
prints the string “ENTER THE LENGTH OF RECTANGLE :” to the screen.ACCEPT nLen
waits for the user to enter a value, which is then stored in the variable nLen.DISPLAY "ENTER THE BREADTH OF RECTANGLE : "
prints the string “ENTER THE BREADTH OF RECTANGLE :” to the screen.ACCEPT nBrd
waits for the user to enter a value, which is then stored in the variable nBrd.nAre=nLen*nBrd
calculates the area of the rectangle by multiplying the value of nLen and nBrd and assigns the result to the variable nAre.DISPLAY "AREA OF RECTANGLE : " nAre
prints the string “AREA OF RECTANGLE :” and the value of the nAre variable.END
is a marker that indicates the end of the program.
This pseudocode prompts the user to enter the length and breadth of a rectangle and then calculates the area by multiplying them. The area is then stored in the nAre variable and the program then prints the area. The program follows the basic flow of input, computation and output.
Example 5: WRITE A PSEUDOCODE TO FIND THE PERIMETER OF RECTANGLE.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nLen,nBrd,nAre display "ENTER THE LENGTH OF RECGTANGLE : " accept nLen display "ENTER THE BREADTH OF RECTANGLE : " accept nBrd nAre=2*(nLen+nBrd) display "PERIMETER OF RECTANGLE : " nAre end |
The above block of text is a pseudocode that calculates the perimeter of a rectangle by prompting the user to enter the length and breadth of the rectangle and then calculates and displays the perimeter. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nLen,nBrd,nAre
declares three variables, nLen, nBrd, and nAre, as numeric data types.DISPLAY "ENTER THE LENGTH OF RECTANGLE : "
prints the string “ENTER THE LENGTH OF RECTANGLE :” to the screen.ACCEPT nLen
waits for the user to enter a value, which is then stored in the variable nLen.DISPLAY "ENTER THE BREADTH OF RECTANGLE : "
prints the string “ENTER THE BREADTH OF RECTANGLE :” to the screen.ACCEPT nBrd
waits for the user to enter a value, which is then stored in the variable nBrd.nAre=2*(nLen+nBrd)
calculates the perimeter of the rectangle by multiplying 2 with the sum of nLen and nBrd, and assigns the result to the variable nAre.DISPLAY "PERIMETER OF RECTANGLE : " nAre
prints the string “PERIMETER OF RECTANGLE :” and the value of the nAre variable.END
is a marker that indicates the end of the program.
This pseudocode prompts the user to enter the length and breadth of a rectangle and then calculates the perimeter by 2*(nLen+nBrd) . The perimeter is then stored in the nAre variable, but it should be named nPerimeter for clarity, and the program then prints the perimeter . The program follows the basic flow of input, computation and output.
Example 6: WRITE A PSEUDOCODE TO FIND 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 |
The above block of text is a pseudocode that calculates the area of a square by prompting the user to enter the length of one side of the square and then calculates and displays the area. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nSide, nArea
declares two variables, nSide, and nArea, as numeric data types.DISPLAY "ENTER THE SIDE OF SQUARE : "
prints the string “ENTER THE SIDE OF SQUARE :” to the screen.ACCEPT nSide
waits for the user to enter a value, which is then stored in the variable nSide.nArea=nSide*nSide
calculates the area of the square by multiplying the value of nSide with itself and assigns the result to the variable nArea.DISPLAY "AREA OF SQUARE : " nArea
prints the string “AREA OF SQUARE :” and the value of the nArea variable.END
is a marker that indicates the end of the program.
This pseudocode prompts the user to enter one side of a square and then calculates the area by squaring the side. The area is then stored in the nArea variable and the program then prints the area. The program follows the basic flow of input, computation and output.
Example 7: WRITE A PSEUDOCODE TO FIND THE PERIMETER OF SQUARE.
1 2 3 4 5 6 7 8 9 | begin numeric nSide,nPeri display "ENTER THE SIDE OF SQUARE : " accept nSide nPeri=nSide*nSide display "AREA OF SQUARE : " nPeri end |
The above block of text is a pseudocode that calculates the area of a square by prompting the user to enter the length of one side of the square, but it is incorrectly displaying the result as the perimeter of the square. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nSide, nPeri
declares two variables, nSide, and nPeri, as numeric data types.DISPLAY "ENTER THE SIDE OF SQUARE : "
prints the string “ENTER THE SIDE OF SQUARE :” to the screen.ACCEPT nSide
waits for the user to enter a value, which is then stored in the variable nSide.nPeri=nSide*nSide
calculates the area of the square by multiplying the value of nSide with itself and assigns the result to the variable nPeri.DISPLAY "AREA OF SQUARE : " nPeri
prints the string “AREA OF SQUARE :” and the value of the nPeri variable, which should be labeled as nArea.END
is a marker that indicates the end of the program.
This pseudocode is not correctly calculating the perimeter of square, it’s just multiplying the side with itself and it incorrectly calls the result as the area of the square.
Example 8: WRITE A PSEUDOCODE TO FIND THE AREA OF CIRCLE.
1 2 3 4 5 6 7 8 9 | begin numeric nRad, nAre display "ENTER THE RADIUS OF CIRCLE : " accept nRad nArea = nRad*nRad*22/7 display "AREA OF CIRCLE : " nArea end |
The above block of text is a pseudocode that calculates the area of a circle by prompting the user to enter the radius of the circle, and then calculates and displays the area. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nRad, nArea
declares two variables, nRad, and nArea, as numeric data types.DISPLAY "ENTER THE RADIUS OF CIRCLE : "
prints the string “ENTER THE RADIUS OF CIRCLE :” to the screen.ACCEPT nRad
waits for the user to enter a value, which is then stored in the variable nRad.nArea = nRad*nRad*22/7
calculates the area of the circle by multiplying the value of nRad with itself, and multiplying with the approximation value of π(22/7), and assigns the result to the variable nArea.DISPLAY "AREA OF CIRCLE : " nArea
prints the string “AREA OF CIRCLE :” and the value of the nArea variable.END
is a marker that indicates the end of the program.
This pseudocode prompts the user to enter the radius of a circle and then calculates the area by using the formula nArea = nRad * nRad * π
, where nArea is the area of the circle, nRad is the radius of the circle and π is the mathematical constant. In this case, π has been approximated to 22/7, so the final formula used is nArea = nRad * nRad * 22/7
. It should be noted that this approximation of π to 22/7 is not accurate, if you need more precision in your calculation, it’s suggested to use the built-in constant for π in the programming language of your choice or use more accurate value of π. It’s also good to name variable and function as per their intended use to make it more readable.
Example 9: WRITE A PSEUDOCODE TO FIND THE CIRCUMFERENCE OF CIRCLE.
1 2 3 4 5 6 7 8 9 | begin numeric nRad,nCir display "ENTER THE RADIUS OF CIRCLE : " accept nRad nCir=2*nRad*22/7 display "CIRCUMFERENCE OF CIRCLE : " nCir end |
The above block of text is a pseudocode that calculates the circumference of a circle by prompting the user to enter the radius of the circle, and then calculates and displays the circumference. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nRad,nCir
declares two variables, nRad and nCir, as numeric data types.DISPLAY "ENTER THE RADIUS OF CIRCLE : "
prints the string “ENTER THE RADIUS OF CIRCLE :” to the screen.ACCEPT nRad
waits for the user to enter a value, which is then stored in the variable nRad.nCir=2*nRad*22/7
calculates the circumference of the circle by multiplying the value of 2 with the value of nRad, multiplying with the approximation value of π(22/7), and assigns the result to the variable nCir.DISPLAY "CIRCUMFERENCE OF CIRCLE : " nCir
prints the string “CIRCUMFERENCE OF CIRCLE :” and the value of the nCir variable.END
is a marker that indicates the end of the program.
This pseudocode prompts the user to enter the radius of a circle and then calculates the circumference by using the formula nCir = 2 * nRad * π
, where nCir is the circumference of the circle, nRad is the radius of the circle and π is the mathematical constant. In this case, π has been approximated to 22/7, so the final formula used is nCir = 2 * nRad * 22/7
. As i have mentioned earlier,this approximation of π to 22/7 is not accurate, if you need more precision in your calculation, it’s suggested to use the built-in constant for π in the programming language of your choice or use more accurate value of π. It’s also good to name variable and function as per their intended use to make it more readable.
Example 10: WRITE A PSEUDOCODE TO FIND THE AREA OF PARALLELOGRAM.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nBase, nPerp, nArea display "ENTER THE BASE : " accept nBase display "ENTER THE PERPENDICULAR : " accept nPerp nArea=nBase*nPerp display "AREA OF PARALLELOGRAM : " nArea end |
The above block of text is a pseudocode that calculates the area of a parallelogram by prompting the user to enter the base and the height, and then calculates and displays the area. Here is an explanation of each line:
BEGIN
is a marker that indicates the start of the program.NUMERIC nBase, nPerp, nArea
declares three variables, nBase, nPerp, and nArea, as numeric data types.DISPLAY "ENTER THE BASE : "
prints the string “ENTER THE BASE :” to the screen.ACCEPT nBase
waits for the user to enter a value, which is then stored in the variable nBase.DISPLAY "ENTER THE PERPENDICULAR : "
prints the string “ENTER THE PERPENDICULAR :” to the screen.ACCEPT nPerp
waits for the user to enter a value, which is then stored in the variable nPerp.nArea=nBase*nPerp
calculates the area of the parallelogram by multiplying the value of nBase with the value of nPerp, and assigns the result to the variable nArea.DISPLAY "AREA OF PARALLELOGRAM : " nArea
prints the string “AREA OF PARALLELOGRAM :” and the value of the nArea variable.END
is a marker that indicates the end of the program.
This pseudocode prompts the user to enter the base and height of a parallelogram and then calculates the area by multiplying the base and height. The area is then stored in the nArea variable and the program then prints the area.
The program follows the basic flow of input, computation and output.It’s also good to name variable and function as per their intended use to make it more readable.
Example 11: WRITE A PSEUDOCODE TO FIND THE AREA OF TRIANGLE.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nBase, nHigh, nArea display "ENTER THE BASE OF TRIANGLE : " accept nBase display "ENTER THE HEIGHT OF TRIANGLE : " accept nHigh nArea=nBase*nHigh display "AREA OF TRIANGLE : " nArea end |
This piece of pseudocode calculates the area of a triangle. It starts by declaring three variables: nBase, nHigh, and nArea. These variables are used to store the base and height of the triangle, as well as the calculated area.
The program then prompts the user to enter the base and height of the triangle, by displaying the messages “ENTER THE BASE OF TRIANGLE : ” and “ENTER THE HEIGHT OF TRIANGLE : ” respectively. The input values are then stored in the nBase and nHigh variables.
The program then calculates the area of the triangle by multiplying the value stored in the nBase variable by the value stored in the nHigh variable. The result is stored in the nArea variable.
Finally, the program displays the message “AREA OF TRIANGLE : ” followed by the value stored in the nArea variable. This displays the calculated area of the triangle to the user.
It’s worth noting that this pseudocode does not include any error handling mechanism in case the user inputs non-numeric value, it will cause the program to crash. Additionally, the program does not include any mechanism for ensuring that the input is a positive value, which could lead to calculating the area of a triangle with negative values.
Example 12: WRITE A PSEUDOCODE TO FIND THE AREA OF RHOMBUS.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nDgn1, nDgn2, nArea display "ENTER THE LENGTH OF FIRST DIAGONAL : " accept nDgn1 display "ENTER THE LENGTH OF SECOND DIAGONAL : " accept nDgn2 nArea=nDgn1*nDgn2/2 display "AREA OF RHOMBUS : " nArea end |
This piece of pseudocode calculates the area of a rhombus. A rhombus is a type of parallelogram that has opposite sides that are congruent and parallel, and its diagonals bisect each other at right angles. The area of a rhombus can be calculated by multiplying the length of one diagonal by the length of the other diagonal and then dividing the result by 2.
The program starts by declaring three variables: nDgn1, nDgn2 and nArea. These variables are used to store the length of the first diagonal, the length of the second diagonal, and the calculated area of the rhombus.
The program then prompts the user to enter the length of the first diagonal and the second diagonal by displaying the messages “ENTER THE LENGTH OF FIRST DIAGONAL : ” and “ENTER THE LENGTH OF SECOND DIAGONAL : ” respectively. The input values are then stored in the nDgn1 and nDgn2 variables.
The program then calculates the area of the rhombus by multiplying the value stored in the nDgn1 variable by the value stored in the nDgn2 variable and then dividing by 2. The result is stored in the nArea variable.
Finally, the program displays the message “AREA OF RHOMBUS : ” followed by the value stored in the nArea variable. This displays the calculated area of the rhombus to the user.
As before, this pseudocode does not include any error handling mechanism in case the user inputs non-numeric value, it will cause the program to crash. Additionally, the program does not include any mechanism for ensuring that the input is a positive value, which could lead to calculating the area of a rhombus with negative values.
Example 13: WRITE A 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 |
This pseudocode prompts the user to enter two numbers, nNum1 and nNum2, and then compares them to determine which is the greater number.
BEGIN is a marker that indicates the start of the program.
NUMERIC nNum1, nNum2 declares two variables, nNum1 and nNum2, as numeric data types.
DISPLAY “ENTER THE FIRST NUMBER : ” prints the string “ENTER THE FIRST NUMBER :” to the screen.
ACCEPT nNum1 waits for the user to enter a value, which is then stored in the variable nNum1.
DISPLAY “ENTER THE SECOND NUMBER : ” prints the string “ENTER THE SECOND NUMBER :” to the screen.
ACCEPT nNum2 waits for the user to enter a value, which is then stored in the variable nNum2.
IF(nNum1>nNum2) is a conditional statement that checks if nNum1 is greater than nNum2. If it is, the code block within the IF statement will be executed.
BEGIN starts the code block that is executed if nNum1 is greater than nNum2.
DISPLAY “GREATEST ONE : ” nNum1 prints the string “GREATEST ONE : ” and the value of the nNum1 variable.
END is a marker that indicates the end of the code block within the IF statement.
ELSE starts the code block that is executed if nNum1 is not greater than nNum2.
BEGIN starts the code block within the ELSE statement.
DISPLAY “GREATEST ONE : ” nNum2 prints the string “GREATEST ONE : ” and the value of the nNum2 variable.
END is a marker that indicates the end of the code block within the ELSE statement.
END is a marker that indicates the end of the program.
Overall, this pseudocode prompts the user to enter two numbers, compares them to determine which is greater, and then prints the greatest number. The program follows the basic flow of input, computation, and output.
Example 14: WRITE A PSEUDOCODE TO FIND THE GREATEST OF TWO NUMBERS USING TERNARY OPERATOR.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nNum1, nNum2 display "ENTER THE FIRST NUMBER : " accept nNum1 display "ENTER THE SECOND NUMBER : " accept nNum2 nGone=(nNum1>nNum2)?nNum1:nNum2 display "GREATEST ONE : " nGone end |
This pseudocode is similar to the previous one, but it uses a ternary operator ( ?: ) to determine the greater number and assigns it to the variable nGone.
BEGIN is a marker that indicates the start of the program.
NUMERIC nNum1, nNum2 declares two variables, nNum1 and nNum2, as numeric data types.
DISPLAY “ENTER THE FIRST NUMBER : ” prints the string “ENTER THE FIRST NUMBER :” to the screen.
ACCEPT nNum1 waits for the user to enter a value, which is then stored in the variable nNum1.
DISPLAY “ENTER THE SECOND NUMBER : ” prints the string “ENTER THE SECOND NUMBER :” to the screen.
ACCEPT nNum2 waits for the user to enter a value, which is then stored in the variable nNum2.
nGone=(nNum1>nNum2)?nNum1:nNum2 uses the ternary operator ( ?: ) to check if nNum1 is greater than nNum2. If it is, nNum1 is assigned to nGone, otherwise nNum2 is assigned to nGone.
DISPLAY “GREATEST ONE : ” nGone prints the string “GREATEST ONE : ” and the value of the nGone variable, which will be the greater of nNum1 and nNum2.
END is a marker that indicates the end of the program.
Overall, this pseudocode prompts the user to enter two numbers, uses a ternary operator to determine the greater number and assigns it to the variable nGone, and then prints the greatest number. The program follows the basic flow of input, computation, and output. And this is a more concise way to determine the greater number and assign it to a variable.
Example 15: WRITE A 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 |
This pseudocode prompts the user to enter three numbers, nNum1, nNum2, and nNum3, and then uses nested if-else statements to determine the greatest number among them.
BEGIN is a marker that indicates the start of the program.
NUMERIC nNum1,nNum2,nNum3 declares three variables, nNum1, nNum2, and nNum3, as numeric data types.
DISPLAY “ENTER THE FIRST NUMBER : ” prints the string “ENTER THE FIRST NUMBER :” to the screen.
ACCEPT nNum1 waits for the user to enter a value, which is then stored in the variable nNum1.
DISPLAY “ENTER THE SECOND NUMBER : ” prints the string “ENTER THE SECOND NUMBER :” to the screen.
ACCEPT nNum2 waits for the user to enter a value, which is then stored in the variable nNum2.
DISPLAY “ENTER THE THIRD NUMBER : ” prints the string “ENTER THE THIRD NUMBER :” to the screen.
ACCEPT nNum3 waits for the user to enter a value, which is then stored in the variable nNum3.
IF(nNum1>nNum2) is a conditional statement that checks if nNum1 is greater than nNum2. If it is, the code block within the IF statement will be executed.
BEGIN starts the code block that is executed if nNum1 is greater than nNum2.
IF(nNum1>nNum3) is a nested conditional statement that checks if nNum1 is also greater than nNum3. If it is, the code block within the nested IF statement will be executed.
BEGIN starts the code block that is executed if nNum1 is greater than nNum3.
DISPLAY “GREATEST ONE : ” nNum1 prints the string “GREATEST ONE : ” and the value of the nNum1 variable.
END is a marker that indicates the end of the code block within the nested IF statement.
ELSE starts the code block that is executed if nNum1 is not greater than nNum3.
BEGIN starts the code block within the ELSE statement.
DISPLAY “GREATEST ONE : ” nNum3 prints the string “GREATEST ONE : ” and the value of the nNum3 variable.
END is a marker that indicates the end of the code block within the ELSE statement.
END is a marker that indicates the end of the code block within the IF statement.
ELSE starts the code block that is executed if nNum1 is not greater than nNum2.
IF(nNum2>nNum3) is a nested conditional statement that checks if nNum2 is greater than nNum3. If it is, the code block within the nested IF statement will be executed.
BEGIN starts the code block that is executed if nNum2 is greater than nNum3.
DISPLAY “GREATEST ONE : ” nNum2 prints the string “GREATEST ONE : ” and the value of the nNum2 variable.
END is a marker that indicates the end of the code block within the nested IF statement.
ELSE starts the code block that is executed if nNum2 is not greater than nNum3.
BEGIN starts the code block within the ELSE statement.
DISPLAY “GREATEST ONE : ” nNum3 prints the string “GREATEST ONE : ” and the value of the nNum3 variable.
END is a marker that indicates the end of the code block within the ELSE statement.
END is a marker that indicates the end of the program.
Overall, this pseudocode prompts the user to enter three numbers, nNum1, nNum2, and nNum3, and then uses nested if-else statements to determine the greatest number among them. It compares the three numbers in a series of nested if-else statements to find the greatest number and finally display it. The program follows the basic flow of input, computation, and output.
It’s important to note that this pseudocode is longer and more complex than the previous ones because it uses nested if-else statements to compare three numbers instead of just two. Also, it could use more variable names for better readability.
Example 16: WRITE A 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 17: WRITE A 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 | 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 Num=nNum1 if(Num<nNum2) begin Num=nNum2 end if(Num<nNum3) begin Num=nNum3 end display "GREATEST ONE : " Num end |
Example 18: WRITE A PSEUDOCODE TO CHECK WHETHER THE ENTERED NUMBER IS EVEN OR ODD.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | begin numeric nNum display "ENTER A NUMBER : " accept nNum if(nNum%2==0) begin display "EVEN" end else begin display "ODD" end end |
Example 19: WRITE A PSEUDOCODE TO CHECK EQUIVALENCE OF TWO NUMBERS. USE IF STATEMENT.
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 "THESE ARE EQUAL" end else begin display "THESE ARE NOT EQUAL" end end |
Example 20: WRITE A PSEUDOCODE TO FIND THE SMALLEST OUT 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 "SMALLEST ONE : " nNum1 end else begin display "SMALLEST ONE : " nNum3 end end else if(nNum2<nNum3) begin display "SMALLEST ONE : " nNum2 end else begin display "SMALLEST ONE : " nNum3 end end |
Example 21: WRITE A PSEUDOCODE TO CHECK WHETHER THE ENTERED YEAR IS A LEAP YEAR OR NOT.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | begin numeric nYear display "ENTER THE YEAR " accept nYear if(nYear%4==0) begin display "THIS IS A LEAP YEAR" end else begin display "THIS IS NOT A LEAP YEAR" end end |
Example 22: WRITE A PSEUDOCODE TO DISPLAY THE NAMES OF THE DAYS OF A WEEK.
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 numeric nCode display "ENTER THE DAY CODE : " accept nCode switch(nCode) begin case 1 : display "MONDAY" break; case 2 : display "TUESDAY" break; case 3 : display "WEDNESDAY" break; case 4 : display "THURSDAY" break; case 5 : display "FRIDAY" break; case 6 : display "SATURDAY" break; case 7 : display "SUNDAY" break; default : display "OUT OF RANGE" end end |
Example 23: WRITE A PSEUDOCODE TO DISPLAY MONTH NAME ACCORDING TO THEIR POSITION.
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 32 33 34 35 36 | begin numeric nCode display "ENTER THE MONTH CODE : " accept nCode switch(nCode) begin case 1 : display "JANUARY" break; case 2 : display "FEBRUARY" break; case 3 : display "MARCH" break; case 4 : display "APRIL" break; case 5 : display "MAY" break; case 6 : display "JUNE" break; case 7 : display "JULY" break; case 8 : display "AUGUST" break; case 9 : display "SEPTEMBER" break; case 10 : display "OCTOBER" break; case 11 : display "NOVEMBER" break; case 12 : display "DECEMBER" break; default : display "OUT OF RANGE" break; end end |
Example 24: WRITE A PSEUDOCODE TO DISPLAY THE STUDENT NAME ACCORDING TO THEIR ROLL NO.
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 | begin numeric nRoll display "ENTER STUDENT ROLL : " accept nRoll switch(nRoll) begin case 1 : display "ANKIT" break; case 2 : display "RAHUL" break; case 3 : display "AMIT" break; case 4 : display "GAURAV" break; case 5 : display "NITESH" break; case 6 : display "VISHAL" break; case 7 : display "CHITRANJAN" break; case 8 : display "KAUSHAL" break; case 9 : display "DHIRENDRA" break; default : display "OUT OF RANGE" end end |
Example 25: WRITE A PSEUDOCODE TO PRINT THE FIRST FIVE NUMBERS.
1 2 3 4 5 6 7 8 9 | begin numeric i for(i=1; i<=5; i++) begin display i end end |
Example 26: WRITE A PSEUDOCODE TO DISPLAY NUMBERS FROM 1 TO 15 USING FOR LOOP.
1 2 3 4 5 6 7 8 9 | begin numeric nCtr for(nCtr=1; nCtr<=15; nCtr++) begin display nCtr; end end |
Example 27: WRITE A PSEUDOCODE TO DISPLAY EVEN NUMBERS FROM 1 TO 14.
1 2 3 4 5 6 7 8 9 | begin numeric nCtr for(nCtr=2; nCtr<=14; nCtr=nCtr+2) begin display nCtr end end |
Example 28: WRITE A PSEUDOCODE TO DISPLAY UPTO INTEGERS UPTO N.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nCtr,nNum display "ENTER THE VALUE OF N : " accept nNum for(nCtr=1; nCtr<=nNUm; nNum++) begin display nCtr end end |
Example 29: WRITE A PSEUDOCODE TO DISPLAY ALL EVEN NUMBERS UPTO N.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nNum, nCtr display "ENTER THE VALUE OF N : " accept nNum for(nCtr=2; nCtr<=nNum; nCtr=nCtr+2) begin display nCtr end end |
Example 30: WRITE A PSEUDOCODE TO DISPLAY ALL ODD NUMBERS UPTO N.
1 2 3 4 5 6 7 8 9 10 11 | begin numeric nNum, nCtr display "ENTER THE VALUE OF N : " accept nNum for(nCtr=1; nCtr<=nNum; nNum=nNum+2) begin display nCtr end end |
Example 31: WRITE A PSEUDOCODE TO FIND THE SUM OF SERIES S=1+2+3+….. +N
1 2 3 4 5 6 7 8 9 10 11 12 | begin numeric nNum, nCtr display "ENTER THE VALUE OF N : " accept nNum for(nCtr=1; nCtr<=nNum; nNum=nNum+1) begin nSum=nSum+nCtr end display nSum end |
Example 32: WRITE A PSEUDOCODE TO FIND THE SUM OF SERIES S=1+3+5+…. +N
1 2 3 4 5 6 7 8 9 10 11 12 | begin numeric nNum, nCtr, nSum display "ENTER THE VALUE OF N : " accept nNum for(nCtr=1; nCtr<=nNum; nCtr=nCtr+2) begin nSum=nSum+nCtr end display "SUM OF SERIES : " nSum end |
Example 33: WRITE A PSEUDOCODE TO PRINT THE ALL EVEN BETWEEN 2 AND 100.
1 2 3 4 5 6 7 8 9 | begin numeric nCtr for(nCtr=2; nCtr<=100; nCtr=nCtr+2) begin display nCtr end end |
Example 34: WRITE A PSEUDOCODE TO PRINT THE INTERGERS 1 TO N USING while LOOP.
1 2 3 4 5 6 7 8 9 10 11 12 | begin numeric nCtr=1, nNum display "ENTER THE VALUE OF N : " accept nNum while(nCtr<=nNum) begin display nCtr nCtr++ end end |
Example 35: WRITE A PSEUDOCODE TO PRINT THE ALL EVEN NUMBERS FROM 1 TO N USING while LOOP.
1 2 3 4 5 6 7 8 9 10 11 12 | begin numeric nNum, nCtr=1 display "ENTER THE VALUE OF N : " accept nNum while(nCtr<=nNum) begin display nCtr nCtr=nCtr+2 end end |
Example 36: WRITE A PSEUDOCODE TO PRINT THE ALL ODD NUMBERS FROM 1 TO N USING while LOOP.
1 2 3 4 5 6 7 8 9 10 11 12 | begin numeric nNum, nCtr=1 display "ENTER THE VALUE OF N : " accept nNum while(nCtr<=nNum) begin display nCtr nCtr=nCtr+2 end end |
Example 37: WRITE A PSEUDOCODE TO FIND THE SUM OF SERIES S=1+2+3+…….+N
1 2 3 4 5 6 7 8 9 10 11 12 | begin numeric nNum, nCtr=1, nSum=0 display "ENTER THE VALUE OF N : " accept nNum while(nCtr<=nNum) begin nSum=nSum+nCtr nCtr++ end end |
Example 38: WRITE A PSEUDOCODE TO FIND THE SUM OF SERIES S=1+3+5+……+N
1 2 3 4 5 6 7 8 9 10 11 12 13 | begin numeric nNum, nCtr, nSum=0 display "ENTER THE VALUE OF N : " accept nNum while(nCtr<=nNum) begin nSum=nSum+nCtr nCtr=nCtr+2 end display "SUM OF SERIES : " nSum end |
Example 39: WRITE A PSEUDOCODE TO PRINT THE NUMBERS FROM 1 TO N USING do….while LOOP
1 2 3 4 5 6 7 8 9 10 11 12 13 | begin numeric nNum, nCtr=1 display "ENTER THE VALUE OF N : " accept nNum do begin display nCtr nCtr++ end while(nCtr<=nNum) end |
Example 40: WRITE A PSEUDOCODE TO PRINT ALL EVEN NUMBERS FROM 2 TO N.
1 2 3 4 5 6 7 8 9 10 11 12 13 | begin numeric nNum, nCtr=2 display "ENTER THE VALUE OF N : " accept nNum do begin display nCtr nCtr=nCtr+2 end while(nCtr<=nNum) end |
Example 41: WRITE A PSEUDOCODE TO PRINT ALL ODD NUMBERS FROM 1 TO N.
1 2 3 4 5 6 7 8 9 10 11 12 13 | begin numeric nNum, nCtr=1 display "ENTER THE VALUE OF N : " accept nNum do begin display nCtr nCtr=nCtr+2 end while(nCtr<=nNum) end |
Example 42: WRITE A PSEUDOCODE TO PRINT THE SUM OF SERIES S=1+2+3+….+N
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | begin numeric nNum, nSum=0, nCtr display "ENTER THE VALUE OF N : " accept nNum do begin nSum=nSum+nCtr nCtr=nCtr+1 end while(nCtr<=nNum) display nSum end |
[…] You may also like: Pseudocode Examples […]
from sympy import *
x, y = symbols(‘x y’)
# exp = x**7+y**5
# print(type(exp))
exp = eval(input(“Enter your function: “))
diff_pt_x = input(“Enter the value of point ‘x’: “)
diff_pt_y = input(“Enter the value of point ‘y’: “)
f_0 = exp.subs([(x, diff_pt_x), (y, diff_pt_y)])
f_1 = (x-int(diff_pt_x))*diff(exp, x).subs([(x, diff_pt_x), (y, diff_pt_y)])
f_2 = (y-int(diff_pt_y))*diff(exp, y).subs([(x, diff_pt_x), (y, diff_pt_y)])
linear_f = f_0 + f_1 + f_2
print(linear_f)
d1 = float(input(“Enter the value: “))
d2 = float(input(“Enter the value: “))
f_x = diff(exp, x)
f_y = diff(exp, y)
f_xx = diff(f_x, x)
f_yy = diff(f_y, y)
f_xy = diff(f_x, y)
max_error = ((max(f_xx, f_yy, f_xy))/2)+(d1+d2)**2
print(max_error)
can u write pseudo code for this
Solution 1:
def taylor_approximation(exp, diff_pt_x, diff_pt_y):
f_0 = evaluate exp at x=diff_pt_x and y=diff_pt_y
f_1 = (x-diff_pt_x) * derivative of exp with respect to x at x=diff_pt_x and y=diff_pt_y
f_2 = (y-diff_pt_y) * derivative of exp with respect to y at x=diff_pt_x and y=diff_pt_y
linear_f = f_0 + f_1 + f_2
return linear_f
def maximum_error(exp, diff_pt_x, diff_pt_y, d1, d2):
f_x = derivative of exp with respect to x at x=diff_pt_x and y=diff_pt_y
f_y = derivative of exp with respect to y at x=diff_pt_x and y=diff_pt_y
f_xx = second derivative of exp with respect to x at x=diff_pt_x and y=diff_pt_y
f_yy = second derivative of exp with respect to y at x=diff_pt_x and y=diff_pt_y
f_xy = second derivative of exp with respect to x and y at x=diff_pt_x and y=diff_pt_y
max_error = ((max(f_xx, f_yy, f_xy))/2) + (d1+d2)**2
return max_error
Solution 2:
import sympy
# Define symbols x and y
x, y = symbols(‘x y’)
# Get the expression from the user
exp_str = input(“Enter your function: “)
exp = eval(exp_str)
# Get the values of x and y at the point of differentiation
diff_pt_x = input(“Enter the value of point ‘x’: “)
diff_pt_y = input(“Enter the value of point ‘y’: “)
# Calculate the linear approximation of the function at the given point
f_0 = exp.subs([(x, diff_pt_x), (y, diff_pt_y)])
f_1 = (x-int(diff_pt_x))*diff(exp, x).subs([(x, diff_pt_x), (y, diff_pt_y)])
f_2 = (y-int(diff_pt_y))*diff(exp, y).subs([(x, diff_pt_x), (y, diff_pt_y)])
linear_f = f_0 + f_1 + f_2
print(linear_f)
# Get values for d1 and d2
d1 = float(input(“Enter the value: “))
d2 = float(input(“Enter the value: “))
# Calculate the second partial derivatives of the function
f_x = diff(exp, x)
f_y = diff(exp, y)
f_xx = diff(f_x, x)
f_yy = diff(f_y, y)
f_xy = diff(f_x, y)
# Calculate the maximum error of the linear approximation
max_error = ((max(f_xx, f_yy, f_xy))/2)+(d1+d2)**2
print(max_error)
Solution 3:
here’s the pseudo-code for the given code snippet:
Import the symbols function from sympy
Define the variables x and y using the symbols function
Evaluate the input function and store it in a variable exp
Get the value of x and y at a specific point from the user
Substitute the values of x and y obtained from the user into the expression exp and store the result in a variable f_0
Calculate f_1 using the diff function and the expression for x and y obtained from the user
Calculate f_2 using the diff function and the expression for x and y obtained from the user
Calculate the linear approximation of the function using f_0, f_1, and f_2 and store it in a variable linear_f
Print linear_f
Get values of d1 and d2 from the user
Calculate the partial derivatives of exp with respect to x and y, and the second partial derivative of exp with respect to x and y
Calculate the maximum error using the partial derivatives and d1 and d2
Print the maximum error
[…] Pseudocode Examples – Programming Code Examples […]
Most of your examples are NOT pseudocode. Pseudocode is written in plain English. Your examples appear to be code. As per your rule “Write what you mean, not how to program it,” there shouldn’t be any reference to actual code.