Chapter 3.
Pseudo code and Flowchart
Objectives:
At the end of the lesson, the student will be able:
To solve a problem using pseudo code.
To learn the different symbols used in a flowchart.
To solve a problem using pseudo code and flowchart.
3.1 PSEUDO CODE
[2]It is a methodology that allows a programmer to represent the implementation of a
solution to a particular problem in a readable format. The purpose of the pseudocode is to
represent the solution which can be understood even by a layman with basic level programming
knowledge.
A pseudocode can also be presented by the use of our common algebraic mathematical
expression
Example 1. Determine if a number is even or odd. A number is said to be even if the
remainder of the number after dividing it by 2 is 0. If the remainder is not equal to 0 then
it is odd.
Illustration:
2 – Even
15 - Odd
Pseudo code:
Step 1. Ask for a number.
Step 2. Divide the number by 2 and take its remainder.
Step 3. If the remainder is 0, then the number is even.
Step 4. If the remainder is not equal to 0, then the number is odd.
Step 5. End.
Example 2. Determine if a number is a prime number or a composite number. A prime
number is a number that is divisible only by 1 or itself, while a composite number is a
number that is divisible by a number greater than 1.
Illustration:
5 – Prime Number
9 – Composite Number
Analysis:
From the above statement, it is readily observable that to determine if a number
is prime, we have to start dividing the number from 2 to less than 1 of the number. If no
number from these range can divide the number, then the number is prime otherwise it
is composite. To determine if no number divides it from 2 to less than 1 of the number,
the resulting remainder after each division operation must not be equal to 0.
Pseudo code:
Step 1. Ask for a number.
Step 2. Create a variable whose initial value is 0
Step 3. Divide the number starting from 2 to number – 1.
Step 4. Take the remainder of step 3.
Step 5. Every time a remainder equal to 0 is encountered, add 1 to the variable.
Step 6. Repeat step 3 until you reached number – 1.
Step 7. If the total value of the variable is 0, then the number is prime.
This means that there was no number from 2 to number - 1
that can divide the number.
Step 8. If the total value of the variable is greater than 0, then it is composite.
This means that there was a number greater than 1 within the range that
can divide the number.
Step 9. End.
Example 3. Perfect number. A number is said to be perfect if the sum of all its factors
starting from 1 to number -1 is equal to the number itself. Write a pseudo code that will
ask for a number and determine if the number is perfect or not a perfect number.
Illustration
6 = Perfect number
Analysis:
The factors of 6 starting from 1 to 5 are the following: 1, 2 and 3.
6 = 1 + 2 + 3 (These are the factors)
6=6
The preceding examples were illustrated using words. The solution for this
example will be a combination of words and mathematical expressions. Before
implementing the pseudocode, let us assign variables that will represent our solution.
Note: The author presumes that the reader knows the basic mathematical operators,
variables and mathematical expressions. The mathematical operators are: +, -, *, / and %.
Let num = number being asked
sum = summation of all factors // initial value of 0
factor = variable that represents the factors // initial value of 1 to num - 1
/ -- division operator
% -- modulus division (remainder)
Pseudocode:
Step 1. Ask for a number (num). // num = 6
Step 2: Divide num starting from factor to factor = num - 1.
Step 3: If num % factor is equal to 0
sum = sum + factor
Repeat step 2 until you reached factor = num - 1. // Terminating error.
Step 4. If num % factor is not equal to 0.
Repeat step 2 until you reached factor = num - 1.
Step 5. If sum is equal to num.
num = perfect number
Step 6. If sum is not equal to num.
num = not a perfect number.
Step 7. End
Analysis of the Pseudocode:
Step 2. The factor variable here represents the divisors.
In the case above, the value of num = 6. We are going to start dividing
num = 6 starting from factor = 1 (initial value is 1) to factor = num – 1 or
starting from to 1 to 5.
Step 3. If the remainder of the operation of step 2 is equal to 0, then perform
sum = sum + factor (0 = 0 + 1). The new value of sum would be the result of
sum + factor. Repeat step 3 until the value of factor = num -1 is reached.
Step 4. If the remainder of the operation of step 2 is not equal to 0, then repeat step
2 until factor = num -1 is reached.
Step 5. If the last value of the sum is equal to your input number which is num,
then the number is perfect.
Step 6. If the last value of the sum is not equal to your input number which is num,
then the number is not perfect.
SUMMARY
The foregoing examples demonstrates the use of a pseudocode in solving a problem. As
a reminder, a pseudocode can be a combination of words or mathematical expressions. The key
feature of a pseudocode is to represent the solution in a way where a person with basic
knowledge in programming can understand or interpret the solution of a specific problem. There
is no particular format in writing a pseudocode, what is important is the logical arrangement and
presentation of the solution.
A pseudo code is the first step in writing a program. It is in this stage of development
where programmers discuss possible solutions of a problem expressed in a readable format. A
lot of programs were first developed using a pseudo code.
Make it a practice to first write a pseudo code in solving a problem rather than directly
solving it using the codes. Not writing a pseudo code first is a common mistake for new
programmers, hopefully you don’t make the same mistake.
3.2 Flowchart
A flowchart is a graphical representation of a solution. It shows the different steps of
solving the problem by using flowchart symbols. Flowcharts helps us visualize complex processes
that are difficult to represent using a pseudo code.
Usually, a flowchart is paired by a pseudo code. It makes the solution understandable and
presentable. A common problem with the use of a pseudo code is grammar. Flowchart offsets
this problem.
3.2.1. Different basic flowchart symbols used in programming
SYMBOL NAME PURPOSE
The oval or terminator, is used to
Oval / Terminator represent the start and end of a
specific process. Every flowchart
must begin with this symbol
This is used to represent the input
Input/ Output or output of a specific process.
This is used to represent a basic
Process task or actions in a process.
Mathematical operations are
usually represented in this
symbol.
This symbol represents a
Decision box particular decision involve in the
process. There are instances
where our program will decide to
continue with a specific process or
not.
This is used to represent the
directional flow of the
Arrow process/data or guide the reader
along the flowcharting path.
These are some of the basic flowchart symbols in programming. There are a lot of
advanced flowchart symbols, but the author specifically focuses on these symbols because these
are commonly used in solving a problem.
With these symbols, we now have everything needed to get started on our flowcharting.
3.3 Problem solving using a flowchart.
Let us first use example 1 in demonstrating the flowchart.
Determine if a number is even or odd. A number is said to be even if the remainder of the
number after dividing it by 2 is 0. If the remainder is not equal to 0 then it is odd.
Illustration:
2 – Even
15 – Odd
Flowchart
Analysis of the flowchart
1. The flowchart always begins at the start symbol.
2. Following the data path, it asks for a number to
be stored in the variable num using the input
symbol.
3. The process symbol gets the remainder of num
after dividing it by 2. rem = num % 2 is the
mathematical expression to get the remainder.
4. The decision box determines if rem is equal to
zero or not.
5. If rem is zero (YES), then the output tells the
viewer it is EVEN. End process.
6. If rem is not zero (NO), then the output tells the
viewer it is ODD. End process.
In the subsequent examples, we are going to pair the flowchart with its corresponding
pseudocode to make our solution presentable and understandable.
Example 4. Write a pseudocode and flowchart for a program that will ask for a 3 -digit
number and reverse the order of the number. Exception: the number must not contain a
0.
Illustration:
Input:
123
Output:
reverse order: 321
Analysis of the problem:
Looking into the problem, it is very difficult to directly reverse a 3-digit number
considering the limited capability of the computer. The computer could not directly
reverse it without a specific instruction on how to reverse the order of the 3-digit number.
Let us go back to basic parts of a number and these are: ones, tens, hundreds and
so on an d so forth place values. We need to break the number in terms of its place value
to extract the digits. The only process of extracting the digits in the number is to divide
the number by 10 repeatedly. Extracting the digits, we can print it individually in a reverse
order. The table below illustrates the process of extracting the place value of the digits.
Table 1. The process of extracting place value of the number.
Process Quotient Remainder
123 / 10 12 3
12 / 10 1 2
1 / 10 0 1
Table 1 shows the process on how to extract the place value. Observe that for the
succeeding division process, the original number (123) will be swapped by a new value
which is the quotient (12) for the next division process. This swapping of value terminates
when the quotient is equal to 0.
Now that we have successfully analyzed the method of reversing the order of a 3-
digit number we can proceed with the pseudo code and its flowchart.
Pseudo code:
Step 1. Ask for a number.
Step 2. x = num/10. // 12
Step 3. a = num % 10. //3
Step 4. num = x.
Step 5. y = num / 10. //1
Step 6. b = num % 10. // 2
Step 7. num = y.
Step 8. z = num / 10. //0
Step 9. C = num % 10. // 1
Step 10. Print value of a b c without space.
Flowchart
Analysis of the flowchart
It can be observed that the flowchart has
a top to bottom approach. All necessary
operations were performed in the process
symbol.
The flowchart is quite understandable
because it mirrors the solution presented
in the pseudo code.
3.3.1 Loop or repeat instructions in pseudo code and flowchart
This is one of the powerful features of modern programming. The loop gives us the power
to repeat instructions or tasks. These repeat instructions can be critical in deriving the correct
answer of a specific task. The loop allows the programmer to solve complex problems.
Before proceeding to the pseudo code and flowchart let us first learn how to make a loop
instruction. A loop instruction normally starts with an initial value, a stopping or terminating error
and finally an expression that will allow us to continue and reach the stopping error. These loop
instructions are presented in mathematical form.
Operators necessary for the loop instruction:
These are the necessary operators needed for creating a loop instruction:
= assignment operator
or < for the terminating operator
+ or – to continue
Loop instruction illustration:
i=1;i<6;i=i+1
i = 1 is the initial value of our loop. This value is needed to reach a particular
stopping error.
i < 6, a conditional expression required to terminate or stop our loop.
i = i + 1, a mathematical expression that will allow us to continue.
The next example will show how a loop instruction will be implemented using a pseudo
code and flowchart.
Example 5. Write a flowchart and a pseudo code that will ask for n number of inputs.
From these inputs determine the largest even number.
Illustration:
Enter number of inputs: 5
Enter inputs: 2 2 10 3 7
Largest even number: 10
Analysis of the problem:
The problem is a little bit complex because it involves multiple inputs. There is a
big possibility that there are multiple even numbers present from the input.
Problems like this requires a loop or repeat instruction. A loop occurs when a given
circumstance or instance needs to be repeated to acquire the desired output. In this case,
we have to repeatedly determine the even numbers present and extract the largest even
number.
The first approach is knowing how many times we have to repeat a particular
operation. Normally this is given by the user. From the above problem, the number of
repeat process is n. We already had an algorithm for determining an even number. The
additional algorithm will be for determining the largest even number.
Now that we now the number of repeat process and what to do, we can proceed
with the pseudo code and flowchart.
Pseudo code:
Step 1. Ask for how many inputs. num
Step 2. Create a variable named largest. largest = 0.
Step 3. Create a loop instruction. j = 1; j <=num; j = j + 1
Step 4.
j = 1; j <= num; j = j + 1
ask for a number
if number % 2 is equal to 0 // determining an even number
if number > largest // determining the largest even number
largest = number // swap the value
repeat step 4
if number % 2 is not equal to 0
repeat step 4
Step 5. Print largest
End Process
Analysis
The pseudo code shows that the user was first asked for the desired number of inputs. The
number of inputs represented by the num variable was used as a control variable for the conditional
expression. This will determine how many times we will repeat a certain instruction.
The next process shows the loop instruction j = 1; j <=num; j = j + 1. If this statement is true,
then it will proceed to the next instruction. The next instruction is to ask for a number. The number
will then be determined if it is even or odd by taking its remainder after dividing by 2.
If the remainder is 0, then it will proceed to determine if it is the largest. A variable largest was
created to hold the largest even number. The initial value was 0 because it was presumed at this point
of the program that there were no even number encountered. If an even number was determined
from the inputs (number), then it will be compared to the largest variable. If number is greater than
largest, then swap the values. In this manner, the variable largest will always contain the largest even
number. After this process, it will then go back to the loop instruction until it is false.
If the remainder is not 0, then it will go back to the loop instruction and repeat the instruction inside
the loop instruction.
The loop instruction will be terminated if the conditional instruction is false. Once the loop
instruction is terminated, it will proceed to the next instruction which is to print the value of largest.
The whole process will be terminated after printing the value of largest.
Example 6. Write a flowchart and a pseudocode that will ask for n number of inputs. From
these inputs determine the largest number.
Illustration:
Enter number of inputs: 5
Enter inputs: 10 11 15 4 11
Largest number: 11
Analysis of the problem:
The process will ask for the desired number of inputs. From these inputs we are
to determine which of this is the largest. This can be achieved by first assuming that the
first input is the largest and then compare it with the second, third and so on and so forth.
If the second input is larger than the first, then swap values. This process will be repeated
until we reached and compared the last input. The values will not be swapped if the
condition will not be fulfilled, in this case num1 > largest.
Pseudo code:
Step 1. Ask for how many inputs. num
Step 2. Create a variable named largest.
largest = 0
Step 3. Create a loop instruction. k=1;k<=num;k=k+1
Step 4. Enter a number. num1
if num1 > largest
largest = num1
repeat step 3
Step 5. Print the value of largest
End process.
Flowchart
Analysis of the flowchart
The flowchart shows that the user will input first the desired number of inputs. The
number of inputs is represented by the variable num. This will be the control variable
for our loop instruction.
The next process shows the loop instruction k = 1; k <=num; k = k + 1. If this
statement is true, then it will proceed to the next instruction. The next instruction is
to ask for a number num1. num1 will then be compared to the value of largest. If
num1>largest, then largest = num1 and then go back to loop instruction.
If the conditional expression num1>largest is false, then go back to the loop
instruction.
The loop will terminate if the loop instruction becomes false. The next process is to
print the value of the variable largest and terminate.
SUMMARY
The loop instruction is a key feature in modern programming languages. It allows us to
solve complex problems. Artificial intelligence software will not be possible if not for this feature.
This loop instruction separates modern programming languages from primitive programming
languages.
It is very important to master loop instruction. Always remember that a loop instruction
starts with an initialization, followed by a conditional expression and lastly a mathematical
expression that will allow us to continue with our loop instruction.
Exercises:
1. Write a pseudo code and a flowchart that will ask for n number of inputs. From these
inputs, count the number of even and number present in the inputs.
Illustration: Enter number of inputs: 6
Enter inputs: 10 2 3 5 7 2
There are 3 even numbers.
There are 3 odd numbers.
2. Write a pseudo code and flowchart that will ask for n number of inputs. Count the
number of occurrences of the number from these inputs.
Illustration: Enter number of inputs: 7
Enter inputs: 10 2 10 5 1 1 100
10, Occurrence: 2
1, Occurrence: 2
3. Write a pseudo code and flowchart that will ask for a number. From this number,
determine the largest digit. Exception: the number must not contain a 0.
Illustration: Enter a number: 1997
Largest digit: 9
References:
[1] CMO 87, S. 2020. [Link]
[2] [Link]
a. [Link]
b. www. [Link]
c. A book on C: Programming in C, 4th Edition
Al Kelly and Ira Pohl
d. [Link]
e. C Language Tutorial version 0.042
Gordon Dodrill, Colorado Enterprises