5 Examples of Pseudocodes
In software programming there is a way to model programs or solutions before coding them, and it is through pseudocodes.
A pseudocode is a set of instructions that results from the interpretation of an algorithm, which will then be transformed into a more formal code that will be written in a programming language. This is a kind of basic code, but very detailed.
They should not be confused with algorithms , since the latter are written as a recipe, with a beginning and an end, without detailing more aspects of what the final program will be. While the pseudocodes are more focused on the problem and highlight what needs to be done and that will later be taken into a programming language, such as Java, PHP, etc.
Pseudocodes can be used to model any program. No matter what language it ends up coding under, the goal of this structure is to establish how we will do it and what we will achieve.
Examples of pseudocodes
-
- Example 1: Pseudocode that provides the volume of a cylinder, knowing its height and diameter.
Start
Show “Enter the diameter, in meters”: Ask for D
Show “Enter the height, in meters”: Ask for H
R = D / 2: Pi = 3.141593
V = Pi * (R ^ 2) * H
Show “The volume of the cylinder is”, V, “cubic meters”
End
- Example 2: Program that lets you know if a number is greater, less than or equal to zero.
Program: CompareNumbers
number: NUMBER
Write “Enter a number”
Read NUMBER
HOMEI
IF NUMBER> 0 THEN
write “The number entered is positive”
BUT
IF NUMBER <0 THEN
write “The entered number is negative”
BUT
write “The number is zero”
END YES
Endprogram
- Examples 3: The user must enter two numbers and the sum of both will be displayed.
PROGRAM Add;
VAR
INTEGER Number1, Number2, Result;
BEGINNING
WRITING («Tell me two numbers to add:«);
READ (Number1, Number2);
Result <- Number1 + Number2;
WRITE («The sum is:«, Result);
END PROGRAM
- Example 4: Program that asks for a number and writes its square
PROGRAM Squares_1;
VAR
INTEGER nNumber, nSquare;
BEGINNING
WRITE («Tell me a number»);
READ (nNumber);
nSquare <- SQR (nNumber);
WRITE (nSquare);
END PROGRAM
- Example 5: Program that says the multiplication table of the number entered by the user.
Program: Multiply Table
numerical: t, num, total;
Start
Write «Enter a number»
Read num
HomeFrom
From t = 1 to t = 10 repeat
make total = num * t
Write: total
end from
End
- Example 6: Pseudocode that provides the average price of a product, calculated from its price in three different stores.
Start
Show “Enter the price of the product in the establishment number 1, in
euros ”: Ask for Price1
Show “Enter the price of the product in establishment number 2, in
euros ”: Ask for Price2
Show “Enter the price of the product in establishment number 3, in
euros ”: Request Price3
Average = (Price1 + Price2 + Price3) / 3
Show “The average price of the product is”, Average, “Euros”
End