Final Slide Algorithm and Flowchart
Final Slide Algorithm and Flowchart
Agenda
What Is a Computer?
What is computer program?
Problem Analysis
Problem solving steps
What is Algorithm?
Properties of Algorithm
Advantages of Algorithms
Disadvantages of Algorithms
How to write an algorithm
What is Variable?
Decision Making Statements
Looping
What is Flowchart?
BUILDING BLOCKS OF FLOW CHART
Rules for Drawing a Flowchart
Agenda
Objectives
able to define problem;
able to define algorithm;
write algorithms for simple problems;
explain properties of an algorithm;
the meaning of flowchart;
explain the need of flow chart;
explain different symbols used in flow chart;
draw flow chart for simple problems; and convert a
flow chart into an algorithm and vice versa.
What Is a Computer?
Introduction
Intelligence is one of the key characteristics which differentiate a
human being from other living creatures on the earth. Basic
intelligence covers day to day problem solving and making
strategies to handle different situations which keep arising in day to
day life.
One person goes Bank to withdraw money. After knowing the
balance in his account, he/she decides to withdraw the entire
amount from his account but he/she has to leave minimum balance
in his account.
ALGORITHM AND FLOW CHART
What is Algorithm?
Go to www.facebook.com
Enter Email ID and Password
Click on log in button
1) FINITENESS
2) DEFINITENESS
3) INPUT
4) OUTPUT
5) EFFECTIVENESS
Properties of Algorithm
1) FINITENESS:
2) DEFINITENESS
3) INPUT
4) OUTPUT:
One always expects output/result(expected value/quantities) in terms
of output from an algorithm. The result may be obtained at different
stages of the algorithm. If some result is from the intermediate stage of the
operation then it is known as intermediate result and result obtained
at the end of algorithm is known as end result. The output is expected
value/quantities always have a specified relation to the inputs.
Properties of Algorithm
5) Effectiveness:
Algorithms to be developed/written using basic operations.
Actually operations should be basic, so that even they can in principle be
done exactly and in a finite amount of time by a person, by using paper
and pencil only.
Properties of Algorithm
it is time consuming.
Step 1: start
Step 2: always declare a variable which you want to use.
Step 3: always read a variable which you are using a variable for input.
Step 4: for processing.
Step 5: displaying a variable.
Step 6: stop.
What is Variable?
Step 1: start
Step 2: print "Good Morning”
Step 3: stop
Write an algorithm to find area of a rectangle?
Step 1: start
Step 2: declare variable L as length, B as breadth and area
Step 3: read L and B
Step 4: area = L * B
Step 5: print area
Step 6: stop
Q) Write an algorithm that perform addition of two numbers and
display the result of the operation.
Step 1: start
Step 2: declare variable a ,b and sum
Step 3: read variable a, b
Step 4: sum = a + b
Step 5: display sum
Step 6: stop
Q) Write an algorithm that perform addition of three numbers
and display the result of the operation.
Step 1: start
Step 2: declare variable a ,a, c and sum
Step 3: read variable a, b and c
Step 4: sum = a + b + c
Step 5: display sum
Step 6: stop
Q)Write an algorithm to swap two numbers using two
variables?
Step 1: start
Step 2: declare variable a & b
Step 3: read variable a & b
Step 4: a = a + b
b=a–b
a=a–b
Step 5: display a & b
Step 6: stop
Q)Write an algorithm to swap two numbers using three
variables?
Step 1: start
Step 2: declare variable a, b and c
Step 3: read variable a & b
Step 4: c = a
a=b
b=c
Step 5: display a & b
Step 6: stop
Q) Write an algorithm to find the area of a circle?
Step 1: start
Step 2: declare variable r and area
Step 3: read variable r
Step 4: area = 3.14 * r * r
Step 5: print area
Step 6: stop
Q) Write to generate electricity bill, which input, current
reading, previous reading, rate per unit, & calculate unit
consume & payable amount.
Current Reading(CR), Previous Reading(PR), Rate Per Unit(R),
Unit Consume(UC) and Payable Amount(PA)
Start 1: start
Start 2: declare variable CR, PR, R, UC & PA
Step 3: read variable CR, PR and R
Step 4: UC = CR – PR
PA = UC * R
Step 5: display UC & PA
Step 6: stop
Q) Write an algorithm to calculate simple
interest(S.I)?
P ------- Principal R--------- Rate T---------Time
Step 1: start
Step 2: declare variable a, b, c & avg
Step 3: read variable a, b and c
Step 4: avg =
Step 5: display avg
Step 6: stop
Q) Write an algorithm to calculate sum & product of
any two numbers?
Step 1: start
Step 2: declare variable a, b, sum & prod
Step 3: read variable a & b
Step 4: sum = a + b
prod = a * b
Step 5: display sum & prod
Step 6: stop
Decision Making Statements
1. If -- statement
If(condition)
{
True statement
}
Q) Write an algorithm which input any number from
user & check whether the number is even number?
Step 1: start
Step 2: declare variable a
Step 3: read variable a
Step 4: If(a % 2 == 0)
{
display “a is even number.”
}
Step 5: stop
If --- else
If (condition)
{
True statement
}
Else
{
False statement
}
Q) Write an algorithm which input any number from user &
check whether the number is even number or odd.
Step 1: start
Step 2: declare variable n
Step 3: read variable n
Step 4: If(n % 2 == 0)
{
Display “n is even number”
}
else
{
display” n is odd number”
}
Step 5: stop
Q) Write an algorithm which input two different numbers.
From the user & find greatest among them?
Step 1: start
Step 2: declare variable a & b
Step 3: read variable a & b
Step 4: If(a > b)
{
display” a is greater than b”
}
else
{
display” b is greater than a”
}
Step 5: stop
Nested If----else
If (condition 1)
{
if(condition 2)
{
print
}
else {
print
}
Else if(condition 3)
{
print
}
else {
print
}
Q) Write an algorithm which input any two numbers. From
the user & find greatest among them?
Step 1: start
Step 2: declare variable a & b
Step 3: read variable a & b
Step 4: If(a > b) {
display” a is greater than b”
}
else if(b>a){
display” b is greater than a”
}
else{
display ”a and b are equal”
Step 5: stop
Q) write an algorithm input three different numbers
from the user & find greater among them?
Step 1: start
Step 2: declare variable a, b and c
Step 3: read variable a, b and c
Step 4: If(a > b) {
if ( a > c) {
display “a is greater”
}
else {
display “c is greater”
}
Else if ( b > c) {
display “b is greater”
}
else {
Display” c is greater”
} }
Q) write an algorithm input any three numbers from
the user & find greater among them?
Step 1: start else if (c>a & c>b){
Step 2: declare variable a, b and c print ” c is greater than a and b”
Step 3: read variable a, b and c }
Step 4: if(a > b & a>c){ else if(a == b & a>c){
print ”a is greater than b and c” print ” a and b greater than c”
} }
else if(b>a & b>c){ else if(a == c & a>b){
print ” b is greater than a and c” print ” a and c greater than b”
} }
else{
print ” b and c greater than a”
}
Step 5: Stop
Homework
Q1) write an algorithm to find a number is positive, negative or Zero.
Q2) Write an algorithm which input two different numbers. From the
user & find smallest among them?
Q3) Write an algorithm which input any two numbers. From the user &
find smallest among them?
Q4) write an algorithm input three different numbers from the user &
find smallest among them?
Q5) write an algorithm input any three numbers from the user & find
smallest among them?
Looping
While (condition)
statement
end
Write an algorithm to print 0 4 8 12 16 20
using while loop?
Step 1: start
Step 2: declare integer i
Step 3: assign i = 0
Step 4: while i <= 20
output i
i=i+4
End
Step 5: stop
Write an algorithm to print 20 16 12 8 4 0
using while loop?
Step 1: start
Step 2: declare integer i
Step 3: assign i = 20
Step 4: while i >=0
output i
i=i -4
End
Step 5: stop
Do while Loop
Loop
Statement
Do (condition)
Write an algorithm to print 0 4 8 12 16 20
using do while loop?
Step 1: start
Step 2: declare integer i
Step 3: assign i = 0
Step 4: Loop
output i
i=i+4
Do i <= 20
Step 5: stop
Write an algorithm to print 20 16 12 8 4 0
using do while loop?
Step 1: start
Step 2: declare integer i
Step 3: assign i = 20
Step 4: Loop
output i
i=i -4
Do i >= 0
Step 5: stop
For Loop
for i = 0 to 20 increment/decrement
output i
End
Write an algorithm to print 0 4 8 12 16 20 using
For loop?
Step 1: start
Step 2: declare integer i
Step 3: for i = 0 to 20 step 4
output i
end
Step 4: stop
Write an algorithm to print 20 16 12 8 4 0 using for
loop?
Step 1: start
Step 2: declare integer i
Step 3: for i = 20 to 0 decreasing step 4
output i
end
Step 4: stop
Nested loops
Write an Algorithm to Print the Following pattern.
2468
2468
2468
Step 1: start
Step 2: declare i, j
Step 3: For i = 1 to 3 step 1
For j = 2 to 8 step 2
output j
end
output nexline
end
What is Flowchart?
A flowchart is pictorial(graphical) representation of an algorithm.
Flowchart is very important tool by which we can understand the flow of
algorithm and program very easily.
It is pictorial representation of step by step solution of a problem. Programmer
often uses it as a program planning tool for visually organizing step necessary
to solve a problem.
A Flowchart is a diagram that uses graphic symbols to depict the nature and
flow of the steps in a process. Another name for this tool is "flow diagram.“
A picture is worth of 1000 words. We can understand more from picture than
words.
The flowchart is a diagram which visually presents the flow of data through processing
systems. This means by seeing a flow chart one can know the
operations performed and the sequence of these operations in a system.
Algorithms are nothing but sequence of steps for solving problems.
So a flow chart can be used for representing an algorithm.
A flowchart, will describe the operations (and in what sequence) are
required to solve a given problem.
A flowchart is a type of diagram that represents an algorithm, workflow or
process. The flowchart shows the steps as boxes of various kinds, and their
order by connecting the boxes with arrows. This diagrammatic
representation illustrates a solution model to a given problem.
Flowcharts are used in analyzing, designing, documenting or managing
a process or program in various fields.
BUILDING BLOCKS
OF
FLOW CHART
OR
COMMON SYMBOLS
OF
FLOW CHART
BUILDING BLOCKS OF FLOW CHART
ANSI/ISO
Name Description
Shape
Shows the process's order of
operation. A line coming from
one symbol and pointing at
Flowline
another. Arrowheads are
(Arrowhead)
added if the flow is not the
standard top-to-bottom, left-
to right.
BUILDING BLOCKS OF FLOW CHART
ANSI/ISO
Name Description
Shape
Indicates the beginning and ending
of a program or sub-process.
Represented as a stadium, oval or
rounded (fillet) rectangle. They
Terminal usually contain the word "Start" or
"End", or another phrase
signalling the start or end of a
process, such as "submit inquiry"
or "receive product".
BUILDING BLOCKS OF FLOW CHART
ANSI/ISO
Name Description
Shape
Indicates the process of inputting
Input / and outputting data, as in entering
Output data or displaying results.
Represented as a parallelogram
BUILDING BLOCKS OF FLOW CHART
ANSI/ISO
Name Description
Shape
Represents a set of operations that
Process changes value, form, or location of
data. Represented as a rectangle
BUILDING BLOCKS OF FLOW CHART
ANSI/ISO
Name Description
Shape
ANSI/ISO
Name Description
Shape
Pairs of labelled
connectors replace long or
On-page confusing lines on a
Connector flowchart page.
Represented by a small
circle with a letter inside.
Flowcharts use different shapes of boxes to denote different type of
instructions. ANSI recommended a number of different rules and
guidelines to help standardize the flowcharting process.
Algorithms are represented using flowcharts.
Flowchart symbols are standardized by ANSI.
Flowchart helps to divide a large complex problem into small
manageable ones.
Generally, algorithm is first represented as a flowchart and then
expressed in a programming language
While preparing a flowchart, the sequence, selection and iterative
structures may be used wherever required
Note
Experienced programmers, sometimes write programs without
drawing a flowchart. Beginners should first draw a flowchart to
reduce number of errors in the program.
Rules for Drawing a Flowchart
It should contain only one start and one end symbol
The relevant symbols must be used while drawing a flowchart
The direction of arrows should be top to bottom and left to right
It should be simple and drawn clearly and neatly
Be consistent in using names, variables in the flow chart
ADVANTAGES OF USING
FLOWCHARTS
As we discussed flow chart is used for representing
algorithm in pictorial form. This pictorial representation of
a solution/system is having many advantages. These
advantages are as follows:
1. Communication.
2. Effective analysis.
3. Documentation of program/system.
4. Efficient program maintenance.
5. Coding of the program.
ADVANTAGES OF USING
FLOWCHARTS
1. Communication:
A Flowchart can be used as a better way of
communication of the logic of a system and steps involve
in the solution, to all concerned particularly to the client
of system.
2. Effective analysis:
A flowchart of a problem can be used for effective
analysis of the problem.
ADVANTAGES OF USING
3. FLOWCHARTS
Documentation of program/ system:
Program flowcharts are a vital part of a good program
documentation. Program document is used for various purposes
like knowing the components in the program, complexity of
the program etc.
Selection
Iteration
Sequence
Sequence logic is used for performing instructions one after
another in sequence.
It is simply performing one step after another
Each step is followed in a specific sequence.
Sequence can be thought of as “do this, then do this, then do
this”
Write an algorithm to print “Good Morning”
Step 1: start
Step 2: print "Good Morning”
Step 3: stop
Write an algorithm to find area of a rectangle?
Step 1: start
Step 2: declare variable L as length, B as breadth and
area
Step 3: read L and B
Step 4: area = L * B
Step 5: print area
Step 6: stop
Q) Write an algorithm that perform addition of two numbers and
display the result of the operation.
Step 1: start
Step 2: declare variable a ,b and sum
Step 3: read variable a and b
Step 4: sum = a + b
Step 5: display sum
Step 6: stop
Q) Write an algorithm that perform addition of three numbers
and display the result of the operation.
Step 1: start
Step 2: declare variable a , b, c and sum
Step 3: read variable a, b and c
Step 4: sum = a + b + c
Step 5: display sum
Step 6: stop
Q)Write an algorithm to swap two numbers using two
variables?
Step 1: start
Step 2: declare variable a & b
Step 3: read variable a & b
Step 4: a = a + b
b=a–b
a=a–b
Step 5: display a & b
Step 6: stop
Q) Write an algorithm to find the area of a circle?
Step 1: start
Step 2: declare variable r, area
Step 3: read variable r
Step 4: area = 3.14 * r * r
Step 5: print area
Step 6: stop
Q) Write an algorithm to find the area of a circle?
Step 1: start
Step 2: declare variable r, area
Step 3: read variable r
Step 4: area = 3.14 * r * r
Step 5: print area
Step 6: stop
Q) Write to generate electricity bill, which input, current
reading, previous reading, rate per unit, & calculate unit
consume & payable amount.
Start 1: start
Start 2: declare variable CR, PR,R, UC & PA
Step 3: read variable CR, PR,R
Step 4: UC = CR – PR
PA = UC * R
Step 5: display UC & PA
Step 6: stop
Q) Write an algorithm to calculate simple
interest(S.I)?
P ------- Principal R--------- Rate T---------Time
Step 1: start
Step 2: declare variable a, b, c & avg
Step 3: read variable a, b, c
Step 4: avg =
Step 5: display avg
Step 6: stop
Q) Write an algorithm to calculate average of 3
numbers?
Step 1: start
Step 2: declare variable a, b, c & avg
Step 3: read variable a, b, c
Step 4: avg =
Step 5: display avg
Step 6: stop
Q) Write an algorithm to calculate sum & product of
any two numbers?
Step 1: start
Step 2: declare variable a, b, sum & prod
Step 3: read variable a & b
Step 4: sum = a + b
prod = a * b
Step 5: display sum & prod
Step 6: stop
Selection
Selection logic, also known as decision logic, is used for making
decisions. Selection logic is depicted as either
an IF...THEN...ELSE or IF.....THEN structure.
Selection is the decision-making construct.
It is used to make yes/no or true/false decisions logically.
Selection can be thought of as “if something is true, take this action,
otherwise take that action”.
Q) Write an algorithm which input any number from user &
check whether the number is even number or odd.
Step 1: start
Step 2: declare variable n
Step 3: read variable n
Step 4: If(n % 2 ==0)
{
Display n is even number
}
else
{
display n is odd number
}
Step 5: stop
Q) Write an algorithm which input two different numbers. From
the user & find greatest among them?
Step 1: start
Step 2: declare variable a & b
Step 3: read variable a & b
Step 4: If(a > b){
display a is greater than b
}
else{
display b is smaller than a
}
Step 5: stop
Q) Write an algorithm which input any two numbers. From
the user & find greatest among them?
Step 1: start
Step 2: declare variable a & b
Step 3: read variable a & b
Step 4: If(a > b) {
display” a is greater than b”
}
else if(b>a){
display” b is greater than a”
}
else{
display ”a and b are equal”
Step 5: stop
Q) write an algorithm input three different numbers
from the user & find greater among them?
Step 1: start
Step 2: declare variable a, b, c
Step 3: read variable a, b, c
Step 4: If(a > b) {
if ( a > c) {
display a is greater
}
else {
display c is greater
}
Else if ( b > c) {
display b is greater
}
else {
Display c is greater
} }
Iteration
Iteration logic is also known as Loop. Iteration logic is
used when one or more instructions may be executed
several times depending on some condition.
Iteration
Iteration comes from the word “reiterate”, which means
to repeat
Iteration is a looping construct
Iteration is a combination of decision and sequence and
can repeat steps
Iteration can be thought of as “while something is true,
do this, otherwise stop”
Looping
In computer programming, a loop is a sequence of
instructions that is continually repeated until a certain
condition is reached.
While loop
While (condition)
statement
end
Write an algorithm to print 0 4 8 12 16 20
using while loop?
Step 1: start
Step 2: declare integer i
Step 3: assign i = 0
Step 4: while i <= 20
output i
i=i+4
End
Step 5: stop
Write an algorithm to print 20 16 12 8 4 0
using while loop?
Step 1: start
Step 2: declare integer i
Step 3: assign i = 20
Step 4: while i >= 0
output i
i=i -4
End
Step 5: stop
Do while Loop
Loop
Statement
Do (condition)
Write an algorithm to print 0 4 8 12 16 20
using do while loop?
Step 1: start
Step 2: declare integer i
Step 3: assign i = 0
Step 4: Loop
output i
i=i+4
Do i <= 20
Step 5: stop
Write an algorithm to print 20 16 12 8 4 0
using do while loop?
Step 1: start
Step 2: declare integer i
Step 3: assign i = 20
Step 4: Loop
output i
i=i -4
Do i >= 0
Step 5: stop
For Loop
for i = 0 to 20 step 4
output i
End
Write an algorithm to print 0 4 8 12 16 20 using
For loop?
Step 1: start
Step 2: declare integer i
Step 3: for i = 0 to 20 step 4
output i
end
Step 4: stop
Write an algorithm to print 20 16 12 8 4 0 using for
loop?
Step 1: start
Step 2: declare integer i
Step 3: for i = 20 to 0 decreasing step 4
output i
end
Step 4: stop
Nested loops
Write an Algorithm & Flowchart to Print the Following pattern.
2468
2448
2468
Step 1: start
Step 2: declare I, j
Step 3: For I = 1 to 3 step 1
For j = 2 to 8 step 2
output j
end
output nexline
end
Step 4: stop
What is Program?
Set of instructions. Instruction is a command to the computer to do
some task.
Implementation of Algorithm or flowchart