Final Slide Algorithm and Flowchart - 123546
Final Slide Algorithm and Flowchart - 123546
Agenda
Problem Analysis
Problem solving steps
What is Algorithm?
Properties of Algorithm
Advantages of Algorithms
Disadvantages of Algorithms
How to write an algorithm
ALGORITHM AND FLOW CHART
Objectives
able to define problem;
able to define algorithm;
write algorithms for simple
problems;
explain properties of an algorithm;
Problem Analysis
Problem analysis can be defined as studying a problem to arrive at a
satisfactory solution. To solve a problem successfully, the first step is
to understand the problem. Also the problem must be stated clearly
and accurately without any confuse.
A well-defined problem and clear description of input and output are
important for an effective and efficient solution.
Study the outputs to be generated so that input can be specified.
Design the steps, which will produce the desired result after
supplying the input.
If the problem is very complex, split the problem into multiple sub-
problems. Solve each sub-problem and combine the solutions of all the
sub-problems to at arrive at overall solution to a large problem. This is called
divide and conquer technique.
Problem solving steps
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.
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, 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
Step 1:among
start
them?
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
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 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
Step 4: stop