What Is an Algorithm?
• It is a finite number of clearly described,
unambiguous “doable” steps that can be
systematically followed to produce a desired
result for given input in a finite amount of
time
• There are two main ways that algorithms can
be represented,
Pseudocode
Flowcharts
Pseudocode
• “Pseudo” Imitation or False
• “code” Instructions written in a programming language
• Pseudocode will be a representation that almost looks
like a code written in a programming language.
• It is also called Program Design Language (PDL)
• There are several formats which are used to write pseudo-
codes and most of them take down the structures from
languages such as C, Lisp, FORTRAN, etc.
• It allows you to include several control structures such
as While, If-then-else, Repeat-until, for and case, which is
present in many high-level languages
• It should be concise and the keyword should be in capital
letter.
Flowchart
• It is the graphical or pictorial representation of an algorithm
General Rules for flowcharting
• Only one flow line is used in conjunction with
terminal symbol.
• Only one flow line should come out from a
process symbol.
• Only one flow line should enter a decision symbol,
but two or three flow lines, one for each possible
answer, should leave the decision symbol.
General Rules for flowcharting
• Write within standard symbols briefly. As necessary,
you can use the annotation symbol to describe data
or computational steps more clearly.
• Avoid the intersection of flow.
• If the flowchart becomes complex, it is better to use
connector symbols to reduce the number of flow
lines
• Ensure that the flowchart has a logical start and
finish
• It is useful to test the validity of the flowchart by
passing through it with a simple test data.
Building Block of Algorithm
Sequence structure
Decision Structure or Selection Structure
Repetition or Iteration Structure
Sequence structure
• This is the most elementary structure
• In this, the steps in an algorithm are
constructed in such a way that, no
condition step is required
• It is the logical equivalent of a straight
line.
Problem: Find the average of six numbers, given
the sum of the numbers.
Pseudocode: Flowchart:
START Terminal
READ the sum
CALCULATE Average = sum / 6 Input
OUTPUT the average
STOP Process
Output
Terminal
Decision Structure or Selection Structure
• It is the case where in the algorithm, one has to make a choice of
two alternatives by making decision depending on a given
condition.
• Selection structures are also called case selection structures when
there are two or more alternatives to choose from
Flowchart Pseudocode
IF condition is true THEN
Do task A
ELSE
Do Task-B
ENDIF
Flowchart Pseudocode
IF condition is true THEN
Do Task-A
END IF
Problem: Determine and Output Whether
Number N is Even or Odd
Flowchart Pseudocode
START
READ number N
IF N modulo 2 is equal to 0 THEN
Display “The number N is even”
ELSE
Display “The number N is odd”
STOP
Repetition or Iteration Structure
• This structure causes the certain steps to be
repeated
• The Repetition structure can be implemented
using,
The While Loop
The For Loop
Repeat Until Loop
• Any program instruction that repeats some
statement or sequence of statements a number of
times is called iteration or a loop
• The commands used to create iterations or loops
are all based on logical tests
Pseudocode : (WHILE loop)
WHILE (a condition is true)
A statement or block of
statements
ENDWHILE
Pseudocode :( DO WHILE loop)
DO
A statement or block of
statements
WHILE (a condition is true)
Pseudocode (FOR loop):
FOR (Initialization, stopping condition, increment/decrement)
Statements
ENDFOR
Pseudocode (REPEAT UNTIL):
REPEAT
A statement or block of statements
UNTIL condition
Problem: To print from 1 to 20
WHILE FOR DO WHILE REPEAT UNTIL
SET x=1 SET x=1 SET x=1 SET x=1
WHILE (x<=20) FOR(x, x<=20, x=x+1) DO REPEAT
PRINT x PRINT x PRINT x PRINT x
x=x+1 END FOR x=x+1 x=x+1
END WHILE WHILE (x<=20) UNTIL (x=21)
Store Check-out Process
Convert Temperature from Fahrenheit (℉) to Celsius (℃)
Pseudocode:
START
READ temperature in Fahrenheit (F)
CALCULATE Celcius (C) temperature
with formula C=5/9*(F-32),
PRINT C
STOP
Determine Whether a Temperature is Below or
Above the Freezing Point
Pseudocode:
START
INPUT temperature
IF (temperature <32) THEN
PRINT "below freezing point"
ELSE
PRINT "above freezing point"
STOP
Print sum of first 100 natural numbers
Pseudocode:
START
SET N=1, Sum=0
WHILE N<=100
Sum=Sum+N
N=N+1
END WHILE
PRINT Sum
STOP