2003291225Algorithm and Flowchart
2003291225Algorithm and Flowchart
To solve any problem through computer we have to take help of computer program. Computer programs are
collections of instructions that tell a computer how to interact with the user, interact with the computer hardware and
process data and finally solve the said problem.
Algorithm:
The word ‘Algorithm’ has originated from the word ‘algorism’, which refers to the ‘art of computing’. An
algorithm, is defined as a “well-ordered collection of unambiguous and effectively computable operations, that
when executed, produces a result and halts in a finite amount of time.” In other word, an algorithm is a step-by-step
procedure to solve a given problem.
Characteristics of an Algorithm
Well-ordered: the steps are in a clear order
Unambiguous: the operations described are understood by a computing agent without
further simplification
Effectively computable: the computing agent can actually carry out the operation
Finiteness: must terminate after a finite number of steps
Completeness: must be general so that it can solve any problem of a particular type for which it is designed
Efficiency of an algorithm
How fast the algorithm solves the problem.
How accurate result the algorithm can compute.
Small error in the input data should not produce large errors in the output data.
Flowcharts:
Flowchart is a graphical tool that diagrammatically depicts the steps and structure of an algorithm or program. A
flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows - each shape represents a
step in the process, and the arrows show the order in which they occur. Flowcharting combines symbols and flow
lines, to show figuratively the operation of an algorithm.
Flowcharting Symbols
There are 6 basic symbols commonly used in flowcharting of assembly language programs: Terminal, Process,
input/output, Decision, Connector and Predefined Process. This is not a complete list of all the possible flowcharting
symbols, it is the ones used most often in the structure of Assembly language programming.
YES
Connector Allows the flowchart to be drawn without
X
intersecting lines or without a reverse
flow.
START
INPUT
TASK 1
TASK 2
TASK n
OUTPUT
END
Sequential Structure
Is
Condition
true?
TASK A TASK B
Conditional Structure
(iii) LOOP Structure:
This is also known as repetitive structure. Any repetitive structure mainly consists of a loop or repetition. Actually,
in this structure one or few instruction statements can be executed repeatedly until a condition evaluates to TRUE or
FALSE. It is of two types. One is DO WHILE and another is DO UNTIL.
FALSE
Condition
TRUE
TASK
DO WHILE
TASK
TRUE
Condition
FALSE
DO UNTIL
Example 1: Convert temperature from centigrade to Fahrenheit. (Hints:To convert Centigrade to Fahrenheit,
multiply by 1.8 and add 32 degrees.)
Algorithm: Step 1 : start
Setp 2 : read temperature in centrigrade
Step 3 : caluculate Fahrenheit = 32 + (centigrade * (1.8));
Step 4 : display centigrade and Fahrenheit
Step 5 : stop
Flow chart:
START
Read C
F = 32+(C*1,8)
Display C, F
END
Example 2: Write down an algorithm and draw a flowchart to find and print the largest of three numbers.
Flowchart:
START
Input
N1, N2, N3
Max = N1
is No
N2>Max?
Yes
Max = N2
is No
N3>Max?
Yes
Max = N3
Print
“Largest Number is”, Max
END
Example 3: Write an algorithm and draw a flowchart to calculate the factorial of a number (N). Verify your result
by a trace table by assuming N = 5. (Hint: The factorial of N is the product of numbers from 1 to N)
Trace Table:
Flowchart:
N Factor Counter Decision Print
START
5 1 1 Y
1 2 Y
Input 2 3 Y
N 6 4 Y
24 5 Y
120 6 N 120
Factor = 1
Counter = 1
is N
Counter N
Y
Print Factor
Count = Count +1
STOP
Extra Examples:
Example E.1:To find the roots of the quadratic equation. (Hints: Nature of roots of quadratic equation can be
known from the quadrant = b2-4ac, If b2-4ac >0 then roots are real and unequal, If b 2-4ac =0 then roots are real
and equal, If b2-4ac <0 then roots are imaginary)
Algorithm: Step 1: start
Step 2: read the a,b,c value
Step 3: if (b*b-4ac)>0 then
Root 1= (-b+ pow((b*b-4*a*c),0.5))/2*a
Root 2= (-b-pow((b*b-4*a*c),0.5))/2*a
Step 4: if (b*b-4ac)=0 then
Root1 = Root2 = -b/(2*a)
Step 5: Otherwise Print Imaginary roots. Goto step 7.
Step 6: print roots
Step 7: stop
Flowchart:
START
Read a,b,c
d = pow(b*b-4*a*c),0.5
No
If d > 0
No Yes
If d== 0
R1 = ((-b+D) / (2*a))
Yes
R2 = ((-b-D) /(2*a))
R1=-b / (2 * a )
R2= -b / (2 * a)
Output
R1, R2
Print imaginary
roots
Stop
Example E.2: Write a algorithm and flowchart to check a given number is prime or not. (Hints: Prime number is a
number which is exactly divisible by one and itself only, Ex: 2, 3,5,7,………;)
Algorithm: Step 1: Start
Step 2: Read n
Step 3: m=integer(√𝑛), k=2
Step 4: Dividing n by k
Find the remainder r.
Step 5: If r=0:output is not primed, goto Step 10
Step 6: k=k+1
Step 7: k>m: Exit from loop, goto Step 9
Step 8: Repeat Steps 4 to7
Step 9: Output: n is prime
Step 10: End
Flow chart:
START
Read N
m=int(srt(N)), k=2
q=int(n/k), r=n-q*k
Yes
r=0?
No
k=k+1
Print
“NOT PRIME”
If k>m?
No
Yes
Output
“PRIME”
END
Problem 2: Write an algorithm and draw a flowchart to calculate the area of a triangle having the sides a,
b, c repectively. (Hints: At first check triangle is possible or not)
Problem 3: Write an algorithm and draw a flowchart to multiply two integer without using multiplication
operator.