Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

TM’s C Programming Lecture Notes v1.

4 Algorithm and Flowchart

Lecture Notes:
C Programming
UNIT 1 Part 1
Algorithm and Flowchart
Ver.1.4

1 (For non-commercial educational use only)


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

Table of Contents
1. Algorithm .................................................................................................................................................. 3

1.1 Pseudo code .................................................................................................................................. 3

2. Flowchart .................................................................................................................................................. 3

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.4 Algorithm and Flowchart

1. Algorithm
Def 1: Step-by-step procedure to solve a problem using computer is called 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

Algorithm is generally represented using plain English or using pseudo-code. Pseudo-code


means the representation of algorithm looks like 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).

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


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

2. Flowchart
Def: Pictorial representation of an algorithm is known as flowchart.

3 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.4 Algorithm and 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 flow chart.

2.1 Basic notations used in a flow chart


1: Start:

2 Stop:

3: Input/Output Operations:

4: Processing:

5: Conditional statements (Binary outcome):

4 (For non-commercial educational use only)


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

6: Conditional statements (Multiple outcome):

7. Flow line:

8. Connector:

9. Repeating Operation:

5 (For non-commercial educational use only)


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

3. Writing Algorithm and Drawing Flowchart for Simple


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

Flowchart:

6 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.4 Algorithm and 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:

7 (For non-commercial educational use only)


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

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

3.5 Flowchart to find largest of given two numbers:

8 (For non-commercial educational use only)


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

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

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.

9 (For non-commercial educational use only)


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

10 (For non-commercial educational use only)


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

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

11 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.4 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.

12 (For non-commercial educational use only)

You might also like