You are on page 1of 13

TM's

Lecture Notes in C Programming (LNCP)


UNIT 1 Part 1
Algorithm and Flowchart
Ver.1.5
TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

Table of Contents
1. Algorithm 3
1.1 Pseudo code 3

2. Flowchart 4
2.1 Basic notations used in a flow chart 4

3. Writing Algorithm and Drawing Flowchart for Simple Exercises 6


3.1 Flow chart for addition of two numbers: 6
3.2 Algorithm and Flowchart for Computing Simple Interest: 6
3.3 Flowchart for computing area of a triangle given three sides: 7
3.4 Flowchart for Extracting Unit’s digit of a given number: 8
3.5 Flowchart to find largest of given two numbers: 8
3.6 Flowchart to check if a given number is +ve or –ve or zero 9
3.7 Algorithm & Flowchart to find sum of first n numbers. 9
3.8 Flowchart to find sum of squares of first n numbers. 11
3.9 Algorithm and Flowchart for finding factorial of given number 12

2 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

​1. Algorithm
Def 1: Step-by-step procedure to solve a problem using a computer is called an algorithm.
Def 2: In Computer Science, A finite sequence of unambiguous steps to solve a problem is
called an algorithm.

Example 1: 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: 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

Algorithms are generally represented using plain English or using pseudo-code.


Pseudo-code means the representation of an algorithm looks as if it is written using a
programming language while it is actually not.

​1.1 Pseudo code


It is an informal way of programming description that does not require any strict
programming language syntax or underlying technology considerations. It is used for
creating an outline or a rough draft of a program (algorithm).

Pseudo-code for example 1 will look like the following:-


PROCEDURE: ADD(number:x, number:y)
number:z = x+y
display:z
END:ADD

3 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

​2. Flowchart
Def: Pictorial representation of an algorithm is known as flowchart.
⇨ It represents the flow of execution of instructions in an algorithm by a computer,
using graphical notations.
⇨ Need to study various symbols or pictures used to represent an algorithm as a flow
chart.

​2.1 Basic notations used in a flow chart


1: Start:

2 Stop:

3: Input/Output Operations:

4: Processing:

4 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

5: Conditional statements (Binary outcome):

6: Conditional statements (Multiple outcome):

7. Flow line:

8. Connector:

9. Repeating Operation (looping):

5 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

​ . Writing Algorithm and Drawing Flowchart for Simple


3
Exercises
​3.1 Flow chart for addition of two numbers:

​3.2 Algorithm and Flowchart for Computing Simple Interest:


Algorithm:
Step 1: Start
Step 2: Input principal 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

6 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

Flowchart:

Exercise 1: Write the algorithm and flowchart for computing the area of a circle.

​3.3 Flowchart for computing area of a triangle given three sides:


​Algorithm:

S
​ tep 1: Start
S
​ tep 2: Input 3 sides of the triangle into a,b,c.
​Step 3: Compute s= (a+b+c)/2
​ tep 4: Compute area = 𝑠 × (𝑠 − 𝑎) × (𝑠 − 𝑏) × (𝑠 − 𝑐)
S
S
​ tep 5: Display value of area
​Step 6: Stop

7 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

3.4 Flowchart for Extracting Unit’s digit of a given number:

8 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

​3.5 Flowchart to find largest of given two numbers:

​3.6 Flowchart to check if a given number is +ve or –ve or zero

9 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

​3.7 Algorithm & Flowchart to find sum of first n numbers.


1. Start
2. Input value of n.
3. Initialize i = 1, sum = 0
4. Repeat as long as i<= n
i. sum = sum + i
ii. i = i + 1
5. Display value of sum.
6. Stop.
Flowchart:

10 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

11 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

​3.8 Flowchart to find sum of squares of first n numbers.

12 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Algorithm and Flowchart

​3.9
Algorithm and Flowchart for finding factorial of given
number
1. Start
2. Input the value of n
3. Initialize i = 1, prod =1
4. Repeat the steps as long as i<=n
a. prod = prod *i
b. i = i+1
5. Display factorial value prod
6. Stop.

Flowchart:

13 (For non-commercial educational use only)

You might also like