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

1-Introduction To Algorithms-Flowcharts-Pseudocodes

Python

Uploaded by

yenah57486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

1-Introduction To Algorithms-Flowcharts-Pseudocodes

Python

Uploaded by

yenah57486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

RAJALAKSHMI ENGINEERING COLLEGE

GE19211-Problem Solving and Programming in Python

Algorithm, Flowchart & Pseudocode


CONTENTS
Algorithm
– Introduction, Properties, Advantages and
Disadvantages
Flowcharts
–Introduction, Symbols, Flowchart Examples
Pseudocode
– Introduction, Pseudocode Examples
MCQ
Practice Questions
Algorithm - Introduction

 It can be defined as sequence of instructions to be carried out in order

to solve a problem.

 Algorithms are written in general English language.

 These are independent of any programming language.


Properties of an Algorithm
1. Finiteness: Must terminate after a finite number of steps
2. Definiteness: Each step must be precisely defined
3. Input: For any operation you perform, need some values
4. Output: Result is always expected out of an operation
5. Effectiveness: Operations must be completed in finite amount of time
Advantages of an Algorithm
 Easy to understand for a person without programming knowledge

 Easy to debug

 Easier for a programmer to convert an algorithm into an program


Disadvantages of an Algorithm

 Algorithms is Time consuming.

 Big tasks are difficult to put in Algorithms.


Types of Algorithms
Few types of algorithm are listed below-
 Greedy algorithm: Solves optimization problems by finding the locally optimal
solution.
 Recursive algorithm: It calls itself repeatedly until it solves a problem.
 Dynamic programming algorithm: Solves problems by dividing them into
subproblems and storing the results of subproblems which are later reused.
 Backtracking algorithm: It is a technique for solving problems recursively by
trying to build a solution incrementally, one piece at a time.
Algorithm Examples: Preparation of Tea

Step 1: Place the fresh water in a kettle.


Step 2: Boil the water to match your style of tea.
Step 3: Put the tea bags in the kettle.
Step 4: Add some milk into the kettle.
Step 5: Add some sugar and stir.
Step 6: Drink the prepared tea.
Algorithm Examples: Voting Eligibility
Step 1: Start
Step 2: Get the age of the person.
Step 3: Check whether age >= 18. If so then goto step 4 else goto step 5.
Step 4: Print “Eligible to vote” and goto step 6.
Step 5: Print “Not eligible to vote”
Step 6: Stop
Algorithm Examples: Check Armstrong Number or Not
Step 1: Start
Step 2: Get num as input from the user
Step 3: Initialize sum=0 and temp=num
Step 4: Find the total number of digits in the input number
Step 5: Loop until temp !=0
Step 5.1: Add sum with the cube of last digit of temp
Step 5.2: temp=temp/10
Step 6: If sum == num then print “Armstrong Number” and goto step 8
Step 7: Else print “Not a Armstrong Number”
Step 8: Stop
Flowchart - Introduction
 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.
 Flowcharts are used in analyzing, designing, documenting or managing a
process or program in various fields.
Flowchart - Advantages

 It provides a clear overview of the entire program.


 It documents the steps followed in an algorithm.
 It provides the facility for coding.
 It shows all major elements and their relationship.
Flowchart - Disadvantages
 Flowcharts are time consuming.
 Little modification in flowchart needs to redraw the whole flowchart.
 There is no standard for explaining the depth of detail that should be included
in flowchart.
Flowchart Symbols
Flowchart – Sequence Control Structure

Statement 1

Statement 2

Statement 3

:
Flowchart – Selection Control Structure

No Yes
Condition

else- then-
statement(s) statement(s)
Flowchart – Repetition Control Structure

yes Loop
Condition
Statement(s)

no
Flowchart Example – Find Age of a Person
Begin

Read birth date

Calculate
Age = current year – birth date

Display
age

End
Flowchart Example – Bigger of Two Numbers
Flowchart Example – Sum Numbers from 1 to 10
Begin

sum = 0
current_number = 1

NO
current_number <= 10? print sum

YES
sum = sum + current_number End
current_number = current_number + 1
Pseudocode - Introduction
 Pseudo is a way of describing an algorithm without using any specific
programming language.
 Pseudo codes uses simple English language to express the logic and flow of
the program.
 It has no graphical or tabular syntax.
Pseudocode - Advantages
 It is very easy to convert a pseudo code into program of actual computer
language.
 Pseudo codes are relatively easy to modify in case of change in the logic of
program.
 Pseudo codes are relatively less time consuming than a flow chart.
Pseudocode - Disadvantages
 No graphical representation is available with pseudo codes.
 There are no standard rules to follow in using pseudo codes.
 Pseudo code is difficult to understand for beginner.
Pseudocode Example - Fibonacci Sum
Initialize n to fifty
Initialize sum to zero
Initialize f1 and f2 to zero
repeat n times
add f1 and f2, store this value in sum
assign f1’s current value to f2
assign sum’s current value to f1
end loop
Pseudocode Example – Factorial of a Number
Fact find_factorial (n)
FOR value = 1 to n
Factorial = factorial * value
END FOR
DISPLAY value of factorial
END Fact
Difference between Algorithm and Pseudocode
An algorithm is a procedure or set of instructions which are followed for
solving a mathematical problem or accomplishing a task. An algorithm may
or may not be written in a programming language.

While a pseudocode is an informal representation of an algorithm which is


free from the programming language.
MCQ - 1
Which of these is not a type of algorithm?
a. Greedy algorithm
b. Dynamic Programming algorithm
c. Recursive algorithm
d. Booths algorithm

Ans: d
MCQ - 2
What is a Flowchart?
a. A way to design a text-based algorithm

b. A specific programming language

c. A diagram that represents a set of instructions

d. A scheme of instructions

Ans: Option c
MCQ - 3
What is the difference between a Flowchart and a Pseudocode?
a. Flowchart is a diagram while the pseudocode is written in a programming
language (e.g. Pascal or Java)

b. A flowchart is textual but the pseudocode is a diagram

c. A flowchart is a schematic description of an algorithm, while pseudocode is a


textual description of an algorithm.

d. A flowchart and a pseudocode are the same

Ans: c
Practice Questions
1. Write an algorithm to find the largest of given three numbers.
2. Write an algorithm to find the reversal of a number.
3. Draw a flowchart to find the sum of the digits of a number.
4. Draw a flowchart to check whether a given number is prime or not.
5. Write a pseudocode to find out whether a number is perfect number or not.
6. Write a pseudocode to find out a number is odd or even number.

You might also like