0% found this document useful (0 votes)
21 views

Final Slide Algorithm and Flowchart

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Final Slide Algorithm and Flowchart

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 107

Problem Solving Techniques

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

 ADVANTAGES OF USING FLOWCHARTS


 Limitations of using flowcharts
 Programming Constructs
 What is Program?
ALGORITHM AND FLOW CHART

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?

 A computer is a programmable device that stores, retrieves, and


processes data. The term "computer" was originally given to humans (human
computers) who performed numerical calculations using mechanical calculators,
such as the abacus. The term was later given to mechanical devices
as they began replacing human computers. Today's computers are electronic
devices that accept data (input), process that data, produce output, and store
(storage).

 What was the term computer originally used for?


 The word "computer" was first recorded as being used in 1613 and was originally
was used to describe a human who performed calculations.
What is computer program?

 A computer program is a set of instructions in a programming language for a


computer to execute.
 MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are examples of
computer programs.
 A computer program in its human readable form is called source code. Source
code needs another computer program to execute because computers can only
execute their native machine instructions. Therefore, source code may be
translated to machine instructions using the language’s compiler.
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

 Understand the problem and plan its logic


 Construction of the List of Variables
 Develop an algorithm
 Refine the algorithm. Refining means making changes
 Program Development
 Testing the Program (solution)
 Validating the Program (solution)
ALGORITHM AND FLOW CHART

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?

 Algorithm can be defined as: “A sequence of activities to be processed for


getting desired output from a given input.”
 Procedure
 A procedure is a finite sequence of well-defined instructions, each of
which can be carried out in a finite amount of time.
 An algorithm is a step-by-step procedure to solve a given problem.
What is Algorithm?
 An algorithm is defined as sequence of steps to solve a problem (task).
 An Algorithm is a step by step method for solving a problem.
 The set of rules that define how a particular problem can be solved in finite number of steps.
 Its purpose is to break a larger
task down so that each step can be carried out without creativity.
 One common example of an algorithm is a recipe, which consists of specific instructions for
preparing a dish/meal.
 Algorithm can be written in any language.
Algorithm to log in to Facebook account

 Go to www.facebook.com
 Enter Email ID and Password
 Click on log in button

 Yes you guessed it right. Its very easy to write algorithm.


 All you have to do is write the correct steps in correct sequence.
MUHAMMAD IBN MUSA AL-
KHWARIZMI
 Muhammad ibn Mūsā al-Khwārizmī (Persian: ‫ ;محمد بن موسى خوارزمی‬c.
780 – c. 850), Formerly Latinized as Algoritmi, was a Persian
scholar who produced works in mathematics, astronomy, & geography
under the support of the Caliph Al-Ma'mun of the Abbasid Caliphate.
Properties of Algorithm
Donald Ervin Knuth has given a list of five properties for an algorithm,
these properties are:

1) FINITENESS

2) DEFINITENESS

3) INPUT

4) OUTPUT

5) EFFECTIVENESS
Properties of Algorithm

1) FINITENESS:

An algorithm must always terminate after a finite number of


steps. It means after every step one reach closer to solution of the
problem and after a finite number of steps algorithm reaches to an end
point.
Properties of Algorithm

2) DEFINITENESS

Each step of an algorithm must be precisely defined. It is done by


well thought actions to be performed at each step of the algorithm. Also
the actions are defined unambiguously for each activity in the algorithm.
Properties of Algorithm

3) INPUT

Any operation you perform need some beginning value/quantities


associated with different activities in the operation. So the
value/quantities are given to the algorithm before it begins.
Properties of Algorithm

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

 Any algorithm should have all these five properties


otherwise it will not fulfil the basic objective of solving a
problem in finite time. As you have seen in previous
examples, every step of an algorithm puts you closer to
the solution
Advantages of Algorithms

 It make logic clear.

 Written in natural language.

 Any body can understand easily

 By using algorithm, the problem is broken down into smaller


pieces or steps hence, it is easier for programmer to convert it
into an actual program.
Disadvantages of Algorithms

 it is time consuming.

 Big tasks are difficult to put in algorithms.

 Difficult to show Looping in algorithms.


How to write an algorithm

 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?

 Variables are symbolic names given to data where the value of


the data stored may change during the execution of the program.
In effect, a variable is a named area of memory used to store data.
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 S.I =


 Step 2: declare variable P, R, T & S.I
 Step 3: read P, R & T
 Step 4: S.I = (P * R * T)/100
 Step 5: display S.I
 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 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

2. If --- else statement

3. Nested if --- else statement


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

 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

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

The American National Standards Institute (ANSI)


set standards for flowcharts and their symbols in the
1960s. The International Organization for
Standardization(ISO) adopted the ANSI symbols in
1970. The current standard was revised in
1985. Generally, flowcharts flow from top to bottom
and left to right.
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

Shows a conditional operation


that determines which one of
the two paths the program will
Decision take. The operation is
commonly a yes/no question or
true/false test. Represented as
a diamond (rhombus).
BUILDING BLOCKS OF FLOW CHART

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.

4. Efficient program maintenance:


 Once a program is developed and becomes operational it needs
time to time maintenance. With help of flowchart maintenance
become easier.
ADVANTAGES OF USING
FLOWCHARTS
5. Coding of the program:

 Any design of solution of a problem is finally converted


into computer program. Writing code referring the
flowchart of the solution become easy.
Limitations of using flowcharts
1) Complexity of logic:
If program logic is complex then flowchart of the program becomes
complicated.

2) alterations and modifications in logic:


Any alterations in the program logic may require re­drawing of
flowchart completely.

3) reuse is not possible:


As the flowchart symbols cannot be typed, always reproduction
of flowchart symbols are required.
Programming Constructs

There are THREE basic programming constructs. They are:


 Sequence

 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 S.I =


 Step 2: declare variable P, R, T & S.I
 Step 3: read P, R & T
 Step 4: S.I = (P * R * T)/100
 Step 5: display S.I
 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 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

 Different algorithms have different performance characteristics to


solve the same problem. Some algorithms are fast. Some are slow.
Some occupy more memory space. Some occupy less memory
space. Some are complex an some algorithms are simple.
 Logically algorithm, flowchart and program are the same.

You might also like