Algorithm and Flowcharts
Algorithm and Flowcharts
A Flowchart
o shows logic of an algorithm
o emphasizes individual steps and their interconnections
o e.g. control flow from one action to the next
Consider the following flowchart for example
Example 2
Write an algorithm and draw a flowchart to convert
the length in feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying
LFT with 30
Print length in cm (LCM)
Example 3
Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.
Pseudocode Print A
• Input the width (W) and Length (L) of a
rectangle
• Calculate the area (A) by multiplying L with W
• Print A
DECISION STRUCTURES
The expression A>B is a logical expression
It describes a condition we want to test
If A>B is true (if A is greater than B) we take the action on left
print the value of A
If A>B is false (if A is not greater than B) we take the action on right
print the value of B
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
Relational Operators
Relational Operators
Print B
> Greater than ³ Greater than or equal to
< Less than £ Less than or equal to
= Is equal to ¹ Not equal to
Example 5
Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying
message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX = VALUE1
else
MAX = VALUE2
endif
Step 3: Print “The largest value is”, MAX
NESTED IFS
Nested ifs are one of the alternatives within an IF–THEN–ELSE statement. It may involve further IF–THEN–ELSE
statement
Example
Write an algorithm that reads three numbers and prints the value of the largest number.
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX = N1 [N1>N2, N1>N3]
else
MAX = N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX = N2 [N2>N1, N2>N3]
else
MAX = N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Exercise: Draw the flowchart of the above Algorithm
Print Max
Loop
A loop is a series of commands that will continue to repeat over and over again
until a condition is met. For example, you want to print your name for five times.
Instead of keeping five output statements you can have loop statement
and one input statement
Algorithm to print your name for five times
Step 1 : count=1
Step 2: while (count <=5)
2.a print “your name”
2.b count=count+1
[end of while]
Exercises on Loop
a) Write an algorithm and draw a flowchart to print 1 to 100 using loop
b) Write an algorithm and draw a flowchart to print all even number between 50 to 100 using loop
c) Write an algorithm and draw a flowchart to print 40 to 10 in reverse order using loop (40 39 38…………………..10)
Important terms
1. Variable: A variable is a storage location and an associated symbolic name (an identifier) which contains some
known or unknown quantity or information, a value. The variable name is the usual way to reference the stored
value.
2. Subroutine: A subroutine is a sequence of program instructions that perform a specific task, packaged as a unit. This
unit can then be used in programs wherever that particular task should be performed. Subprograms may be defined
within programs, or separately in libraries that can
be used by multiple No programs.