Algorithms
Algorithms
Flowcharts
By Manoj Kumar Kar
Basic Definition:
An algorithm is a finite sequence of
step by step, discrete, unambiguous
instructions for solving a particular
problem
• Has input data, and is expected to produce
output data.
• Each instruction can be carried out in a
finite amount of time in a deterministic way.
In simple terms, an algorithm is a
series of instructions to solve a
problem (complete a task).
Algorithm Issues
Algorithm must be specified
◦ Pseudo code is combination of English
and programming language constructs
Algorithmis either exact or
approximate
◦ An exact algorithm produces an output
which is valid for input while an
approximate algorithm produces an
output close to valid output
Algorithm Issues:
Algorithm must be specified:
Algorithm is either exact or
approximate:
An exact algorithm produces an
output which is
valid for input while an approximate
algorithm
produces an output close to valid
output.
Examples:
Write an algorithm to add two
numbers.
Write an algorithm to subtract
two numbers.
Write an algorithm to multiply
two numbers.
Write an algorithm to calculate
the area of a circle.
Write an algorithm to calculate
the area of a rectangle.
Algorithm to add two
numbers:
Step 1: Start.
Step 2: Input the first number A.
Step 3: Input the second number B.
Step 4: Calculate C= A+B.
Step 5: Display the result C.
Step 6: End.
Algorithm to Subtract two
numbers:
Step 1: Start.
Step 2: Input the first number A.
Step 3: Input the second number B.
Step 4: Calculate C= A-B.
Step 5: Display the result C.
Step 6: End.
Algorithm to Multiply two
numbers:
Step 1: Start.
Step 2: Input the first number A.
Step 3: Input the second number B.
Step 4: Calculate C= A*B.
Step 5: Display the result C.
Step 6: End.
Algorithm to calculate area of a
circle
Step 1: Start.
Step 2: Input the radius of a circle
r.
Step 3: Calculate the area A=
3.141 * r * r.
Step 4: Display the area of the
circle.
Step 5: End.
Algorithm to calculate area of a
rectangle
Step 1: Start.
Step 2: Input the length of the
rectangle l.
Step 3: Input the width of the
rectangle w.
Step 4: Calculate the area A= l *
w.
Step 5: Display the area of the
rectangle.
Step 6: End.
The Flowchart
A Flowchart
◦ Shows logic of an algorithm.
◦ Emphasizes individual steps and
their interconnections
◦ e.g. control flow from one action to
the next
Flowchart Symbols
Name Symbol Use in Flowchart
Lcm Lft x
30
Print
Lcm
STOP
Example 2
Write an algorithm and draw a
flowchart that will read the two
sides of a rectangle and calculate
its area.
Pseudo code
Input the width (W) and Length (L) of a
rectangle
Calculate the area (A) by multiplying L
with W
Print A
Example 2
Algorithm START
Print
A
STOP
Example 3
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
Example 5
START
Input
VALUE1,VALUE
2
Y is N
VALUE1>VALUE2
MAX MAX
VALUE1 VALUE2
Print
“The largest value is”,
MAX
STOP
Home Task
Write an algorithm to find the biggest number
among three different numbers and draw the
flow chart for the same.
Write an algorithm to find if a given number is
even or odd, draw the flowchart for the same.
Write an algorithm to find a given year is leap
year or not and draw the flow chart for the
same.
Thank You