You are on page 1of 29

Algorithm and Flowchart

Algorithm
 The term algorithm refer to the logic of a program.Its step-
by-step description of a solution to the given problem.

 Definition :-
A format or finite set of steps for solving a particular
problems is called as algorithm.

 It has clear starting and stopping point. With specific step


by step instructions in each line.
Characteristics of Algorithm
 Each instruction should be precise and clear.
 Each instruction should be executed in a
finite time.
 One or more instructions should not repeated
infinitely.
 After executing the instructions the desired
output are obtained.
Example of Algorithm
 Problem1:- Find the area of Circle of Radius r.
Input to the algorithm:
Radius r of the Circle
Expected output :
Area of the circle

Algorithm
Step1: Start a program.
Step2: Read/input the Radius r of the
Circle
Step3: AreaPI*r*r //calculation of area
Step4: Print Area
Step5: End
Problem2: Write an algorithm to read two numbers and find
their sum.

 Input to the algorithm:


 First num1.
 Second num2.
 Expected output
 Sum of the two numbers.

 Algorithm:

◦ Step1: Start
◦ Step2: Read\input the first num1.
◦ Step3: Read\input the second num2.
◦ Step4: Sum num1+num2 // calculation of sum
◦ Step5: Print Sum
◦ Step6: End
Problem 3: Convert temperature Fahrenheit to Celsius
 Inputs to the algorithm:
◦ Temperature in Fahrenheit
 Expected output:
◦ Temperature in Celsius

Algorithm:
 Step1: Start

 Step 2: Read Temperature in Fahrenheit F

 Step 3: C= 5/9*(F-32)// calculation of F=9/5(C+32)

 Step 4: Print Temperature in Celsius: C

 Step5: End
Types of Algorithms
 The algorithms and flowchart, classification
three types of control strucutres.

1.Sequence
2.Branching(Selection)
3.Loop(Repetition)
Sequence
Each step of the algorithm is executed
in the specified order.

Example

An algorithm to add two numbers

Step 1: Input the first number as A


Step 2: Input the second number as B
Step 3: Set Sum = A+ B
Step 4: print Sum
Step 5: End
Decision
 Decision statement are used when the
execution of a process depends on the
outcome of some condition.
 For example, if x=y, then print “EQUAL”.

Algorithm
Step 1: Input the first number as A
Step 2: Input the first number as B
Step 3: if A=B
then print “Equal”
else
print “Not equal”
Step 4: End
Repetition
 Repetition, which involves executing one or
more steps for a number of times, can be
implemented using constructs such as the
while, do-while, and for loops.

 These loops execute one or more steps until


some condition is true.

 An algorithm that prints the first 10 natural


numbers.
Algorithm to print 10 natural numbers

 Step 1: Initialize the values I=1 and N=10


 Step 2: Repeat Step 3 and 4 while loop I<=N
 Step 3: Print the element I
 Step4: Set the I value is i+1
 End
Flowchart
 Graphical representation of an algorithm.
 It shows the logic of an algorithm.
 It emphasizes individual steps and their

interconnections.
 It controls flow from one action to the next

giving the sequence of instructions.


 It ends after completing the required

processes.
 Uses standard symbols representing different

actions in solving a problem.


Flowchart Symbols
Name Symbol Use in Flowchart
Rounded
Denotes beginning or end of program.
Rectangle

Parallelogram Denotes input/output operation.

Denotes process to be carried out.


Rectangle
Ex: Addition, Subtraction, etc.

Denotes decision (or branch) to be made.


Diamond The program should continue along one of
the two routes. Ex: IF/THEN/ELSE

Flow line Denotes the direction of logic flow in


program.
Pseudocode
 Pseudocode is a short hand way of describing a computer

program.
 It uses programming language specific syntax for representing the

algorithm.
 Using pseudocode, it is easier for a programmer to understand

the general working of the program.


 Algorithm uses English-like words whereas pseudocode uses the

keywords used in programming language.


 Algorithm, flowchart and pseudocode provide the representation

of the procedure followed in solving the problem.


Pseudocode for finding largest of three
numbers
Step 1: Read the values of three numbers Number 1, Number 2 and
Number 3
Step 2: IF the value of Number 1 > Number 2 {
GOTO step 3 }
Else {
GOTO step 4 }
Step 3: IF the value of Number 1 > Number 3 {
Print “The largest value is Number 1”
GOTO step 5 }
Else {
GOTO step 4 }
Step 4: IF the value of Number 2 > Number 3 {
Print “The largest value is Number 2”
GOTO step 5 }
Else {
Print “The largest value is Number 3”
GOTO step 5 }
Step 5: STOP processing
 Write an algorithm to determine a student’s final grade and indicate
whether it is passing or failing. The final grade is calculated as the
average of four marks.

Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/
4
Print “PASS”
Step 4: stop
N IS Y
GRADE<5
0

PRINT PRINT
“PASS” “FAIL”

STOP
1. Write an algorithm and draw a flowchart to
convert the length in feet to centimeter?
2. Write an algorithm and draw a flowchart
that will read the two sides of a rectangle
and calculate its area.
 Write an algorithm and draw a flowchart

that will calculate the roots of a quadratic


equation
◦ Hint: d = sqrt ( b2 -4ac ), and the roots are: x1
= (–b + d)/2a and x2 = (–b – d)/2a
Pseudocode:
 Input the coefficients (a, b, c) of the quadratic
equation
 Calculate d
 Calculate x1
 Calculate x2
 Print x1 and x2
Example 4

START
 Algorithm:
Input
 Step 1: Input a, b, c a, b, c
 Step 2: d  sqrt ( b2 -4ac )
 Step 3: x1  (–b + d) / (2 x a) d  sqrt(b x b – 4 x a x c)
 Step 4: x2  (–b – d) / (2 x a)
x1 (–b + d) / (2 x a)
 Step 5: Print x1, x2
X2  (–b – d) / (2 x a)

Print
x 1 ,x 2

STOP
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
 Write and algorithm and draw a flowchart
to
a) read an employee name (NAME), overtime hours
worked (OVERTIME), hours absent (ABSENT) and
b) determine the bonus payment (PAYMENT).

Bonus Schedule

OVERTIME – Bonus Paid


(2/3)*ABSENT

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40)
then
PAYMENT  50
else if (OVERTIME–(2/3)*ABSENT > 30)
then
PAYMENT  40
else if (OVERTIME–(2/3)*ABSENT > 20)
then
PAYMENT  30
else if (OVERTIME–(2/3)*ABSENT > 10)
then
PAYMENT 20
else
PAYMENT  10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Draw a flowchart to find the sum of first 50 natural numbers

start

SUM = 0

N=0

N=N+1

SUM = SUM
+N

No IS
N=50
?
YES

PRINT SUM

End
Draw a flowchart for computing factorial N (N!)
 start

Read N

M=1
F=1

F=F*M

NO
IS
M=M+1
M=N?
YES

PRINT F

End
/* Sum of n natural numbers */
 step 1. Read the value of n.
Step 2. set values of i = 1 , SUM = 0
step 3. if ( i > n ) go to 7
step 4. S = S + i
step 5. i = i + 1
step 6. go to 3
step 7. Display the value of S
step 8. Stop
/* Factorial of n numbers */
step 1. Read the value of n.
Step 2. i = 1 , F =1
step 3. if ( i > n ) go to step 7
step 4. F = F * i
step 5. i = i + 1
step 6. go to step3
step 7. Display the value of S
step 8. Stop

/* To Find the value of x n */

1. Read the value of n.


2. i=1,m=1
3. If i > n then go to 7
4. m=m*x
5. i=i+1
6. go to 3
7. Display m
8. Stop
QUESTION
 Draw a flowchart to read a number N and

print all its divisors.


 2. Draw a flowchart for computing the sum of

the digits of any given number


 3. Draw a flowchart to find the sum of given

N numbers.
 4. Draw a flowchart to computer the sum of

squares of integers from 1 to 50


 5. Draw a flowchart to arrange the given data

in an ascending order.
Logical thinking and analysis
 Understand and analyze the process of solving
problems.
 Apply appropriate method or strategy such as top
down or bottom up approach. For example,
stepwise refinement is one of the methods used by
dividing the main problem into sub problems and
further sub-divided so that the individual sub
problem may be solved easily.
 Apply the idea or concept or the logic to the
context of the problem and transform the logic as
program code giving solution by using computer.
 Problem to problem, the solution varies and more
number of solutions are available for a single
problem.
 END

You might also like