Algorithm &
Flowchart
C PROGRAMMING ➔ UNIT 1
Algorithm 2
❑ Def 1: Step-by-step procedure to solve a problem using
computer is called algorithm.
❑ Def 2: In Computer Science, A sequence of unambiguous
steps to solve a problem is called an algorithm.
Example 1: 3
❑ Algorithm for adding any two numbers.
Step 1: Start
Step 2: Input the two numbers to be added and assign
them to variables X & Y respectively.
Step 3: Compute value Z such that Z = X + Y
Step 4: Display the value of Z.
Step 5: Stop
Example 2: 4
❑ Algorithm for checking a given number is +ve or –ve
(Assume 0 is positive)
❑ Step 1: Start
❑ Step 2: Input the number and assign it to variable X.
❑ Step 3: Check if X is less than 0, then display “ The
given number is –ve”. Otherwise display “ The given
number is +ve”
❑ Step 4: Stop
Pseudo code 5
❑ It is an informal way of programming description that does
not require any strict programming language syntax.
❑ It is used for creating an outline or a rough draft of a
program (algorithm).
Pseudo-code example 6
❑ Pseudo-code for adding 2 numbers:-
PROCEDURE: ADD(number:x, number:y)
number:z = x+y
display:z
END:ADD
Flowchart 7
Def: Pictorial representation of an
algorithm is known as Flowchart.
❑ Def: Pictorial representation of an algorithm is known
It represents
as Flowchart.the flow of execution of
instructions in an algorithm by a
❑ It represents the flow of execution of instructions in an
computer, using graphical notations.
algorithm by a computer, using graphical notations.
Need to study various symbols or
pictures
❑ Need used tovarious
to study represent
symbolsan or pictures used to
represent
algorithm asanflow
algorithm as flow chart.
chart.
Notations used in flowchart 8
Notations used in flowchart 9
Notations used in flowchart 10
Notations used in flowchart 11
Notations used in flowchart 12
Notations used in flowchart 13
Notations used in flowchart 14
repeat
False as long
as the
conditio
n is true
True
statements
Rest of the code
Elements of user-defined functions 15
❑ Flowline
❑ Connector
Flow chart for addition of two nos. 16
numbers:
Computing Simple Interest 17
❑ Algorithm
Step 1: Start
Step 2: Input principle p, rate of interest r, and time
period t.
Step 3: Compute Simple interest si = (p*r*t)/100
Step 4: Display simple interest si
Step 5: Stop
Computing Simple Interest 18
❑ Flowchart
Area of a triangle(given 3 sides) 19
❑ Flowchart
Extracting Unit’s digit of a given number: 20
❑ Flowchart
Larger of two numbers 21
❑ Flowchart
check if a given number is +ve or –ve or zero22
❑ Flowchart
Sum of first n +ve numbers 23
❑ Algorithm
n = 5,
Step 1: Start
Step 2: Input value of n. i sum
Step 3: Initialize i = 1, sum = 0
1 0
Step 4: Repeat as long as i<= n 2 1
i. sum = sum + i 3 3
ii. i = i + 1 4 6
Step 5: Display value of sum. 5 10
Step 6: Stop. 6 15
Sum of first n +ve 24
numbers
Factorial of given number 25
❑ Algorithm
1. Start
2. Input the value of n
3. Initialize i = 1, fact =1
4. Repeat the steps as long as i<=n is true
I. fact = fact *i
II. i = i+1
5. Display factorial value fact
6. Stop.
Factorial of given 26
number (Flowchart)