Algorithm and Flowchart
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.
Algorithm
Step1: Start a program.
Step2: Read/input the Radius r of the
Circle
Step3: AreaPI*r*r //calculation of area
Step4: Print Area
Step5: End
Problem2: Write an algorithm to read two numbers and find
their sum.
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
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
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.
interconnections.
It controls flow from one action to the next
processes.
Uses standard symbols representing different
program.
It uses programming language specific syntax for representing the
algorithm.
Using pseudocode, it is easier for a programmer to understand
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
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
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
N numbers.
4. Draw a flowchart to computer the sum of
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