Module 2 - Flowcharts and Algorithms
Module 2 - Flowcharts and Algorithms
Module 2 - Flowcharts and Algorithms
Algorithms
BITS Pilani Dr. Jagat Sesh Challa
Pilani Campus
Department of Computer Science & Information Systems
Module Overview
• Flowcharts
• Algorithms
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
• Step5: Debugging
• Step6: Documentation
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Flowcharts
Flow Chart
• A Graphical representation of a solution to a particular
problem
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
How to make a flow chart
Step1: Identify input and output.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Symbols used in Flow Charts
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Flow Chart Example 1:
Sum of two numbers
Start
sum = num1+num2
Display sum
Stop
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Flow Chart Example 2:
Whether a number is even or odd
Start
Read num
No is Yes
num mod 2 == 0
Stop
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Flow Chart Example 3:
Sum of 10 Numbers
Start
Declare variables
n = 1, sum = 0, x = 0
is
Yes No
n <= 10
sum = sum + x
Stop
n=n+1
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Algorithms
Algorithms
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Programming Methodologies
Bottom Particular
The algorithms
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Top-down vs Bottom-up
Programming
• Top – Down Programming:
q Reverse engineers the finished products by breaking it into
smaller, manageable or reusable modules.
q Big picture is visualized first before breaking it up into separate
components.
• Bottom – UP Programming:
q Low level components are designed first without fully knowing the
big picture.
q The components are later integrated to form the complete
system.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
What is an Algorithm?
v Step by step solution to a problem in English like language.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Programming Logic Constructs
• Imperative statements (Actions)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Algorithm Example 1:
Sum of two numbers (Imperative)
Start
Stop
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Algorithm Example 2:
Whether a number is even or odd (Conditionals)
1. START
Start
2. Declare num
Declare variable num
3. INPUT the number num
Read num 4. IF num mod 2 == 0 THEN
No is
num mod 2 == 0
Yes continue ELSE GOTO step 7
Display “number Display “number 5. PRINT “number is even”
is odd” is even”
6. GOTO step 8
Stop
7. PRINT “number is ODD”
8. END
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Algorithm Example 3:
Sum of 10 Numbers (Iterative)
1. START
Start 2. Declare n=1, sum=0, x=0
Declare variables 3. IF n <=10 THEN continue ELSE
n = 1, sum = 0, x = 0
GOTO step 8
Yes is No
4. INPUT x
n <= 10
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Exercise
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
More Exercises
• Draw a flowchart and write its corresponding algorithm for
checking if a number n entered by the user is a prime number
or not. [Hint: Check for all numbers from 1 to n/2 if they divide
n with remainder 0 or not. If none of them divides, n is a prime
number]
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus