1.5 Algorithms
1.5 Algorithms
Introduction to Computer
Programming and Data Structures
Dr. B Ramachandra Reddy
Outline
• Algorithm
• Flow Charts
• Pseudo Code
Program Planning
To write a correct program, a programmer must write each and every
instruction in the correct sequence
Logic (instruction sequence) of a program can be very complex
Hence, programs must be planned before they are written to ensure
program instructions are:
Appropriate for the problem
In the correct sequence
Algorithm
Refers to the logic of a program and a step-by-step description of how to
arrive at the solution of a given problem.
In order to qualify as an algorithm, a sequence of instructions must
have following characteristics:
Each and every instruction should be precise and unambiguous
Each instruction should be such that it can be performed in a finite time
One or more instructions should not be repeated infinitely. This ensures that the
algorithm will ultimately terminate
After performing the instructions, that is after the algorithm terminates, the
desired results must be obtained
Sample Algorithm (Example 1)
Step 8: Stop.
Representation of Algorithms
As flowcharts
As pseudocodes
Flowchart
Yes A=B
I=?
=0 =1 =2 =3 =4 =5 = Other
1. Sequence logic,
2. Selection logic, and
3. Iteration (or looping) logic
Process 1
Process 1
Process 2
Process 2
(a) (b)
Flowchart Pseudocode
Selection Logic
• Also known as decision logic, it is used for making decisions
Yes No IF Condition
IF (condition)
THEN Process 1
THEN ELSE ELSE Process 2
Process 1 Process 2
ENDIF
(a) (b)
Flowchart Pseudocode
Selection Logic (IF…THEN Structure)
Yes No
IF (condition)
IF Condition
THEN
THEN Process 1
Process 1
ENDIF
(a) (b)
Flowchart Pseudocode
Selection Logic (CASE Structure)
Yes
Type 1 Process 1
No
Yes
Type 2 Process 2 CASE Type
Yes
Type n Process n
Case Type n: Process n
No
ENDCASE
(a)
Flowchart (b)
Pseudocode
Iteration (or Looping) Logic
Used to produce loops in program logic when one or more instructions may
be executed several times depending on some conditions
1. DO…WHILE
2. REPEAT…UNTIL
Iteration (or Looping) Logic
(DO…WHILE Structure)
False
Condition?
True
Process 1 DO WHILE Condition
Process 1
Block
Process n
Process n ENDDO
(a) (b)
Flowchart Pseudocode
Iteration (or Looping) Logic
(REPEAT…UNTIL Structure)
Process 1
REPEAT
Process 1
Process n
Process n
False
Condition? UNTIL Condition
True
(a) Flowchart (b)
Pseudocode
Sample Algorithm
Sample Algorithm
Flowchart Vs Pseudo code
Flowchart Pseudocode
Start/End Begin/End
Decision If/if-lse/case/while/
do-while/for
Connector Not Applicable
• Write a pseudo code and flowchart to find the given number is prime
or not?
• Write a pseudo code and flowchart to generate the Fibonacci series
upto n numbers.
• Write a pseudo code and flowchart to check the given number is
palindrome or not?