Programming with C 2017
Algorithms and Flowcharts
Algorithm
It is a step by step procedure to solve a problem
A sequential solution of any problem that is written in human language.
Using the algorithm the programmer can easily writes the actual program.
The main use of algorithm is to help us to translate English into C.
Algorithm consists of combination of statements of English and C, but not exactly C. It can then
be converted to C program.
It is an outline or basic structure or logic of the problem.
Characteristics of Algorithm
Each and every instruction must be precise and unambiguous.
Each instruction execution time should be finite.
There should be a termination condition.
It has to accept 0 or more inputs and produce compulsory an output.
It can accept any type of input and produce a corresponding output.
General Way of Writing the Algorithm
Name of the algorithm must be specified.
Beginning of algorithm must be specified as Start
Input and output description may be included
Step number has to be included for identification
Each step may have explanatory note provided with in square bracket followed by operation.
Completion of algorithm must be specified as end or Stop.
General Steps for Writing the Algorithm
Algorithm: Name of the Algorithm
Input: Input Values / Values to be read
Output: Output corresponding to the input / Result
Step 1: Start
Step 2: Read
Step 3: Logic / Formula
Step 4: Display / Result / Output
Step 5: Stop
1 Mr. Rajesh S M, Assistant Professor
Programming with C 2017
Ex 1: Write an algorithm to add two numbers Ex 2: Write an algorithm to find simple interest
Algorithm: Addition of two numbers Algorithm: To find Simple Interest
Input: Two number a and b Input: p, t, r
Output: Sum of two numbers Output: si
Step 1: Start Step 1: Start
Step 2: [input two number] Step 2: [input a principle p, time t & rate r]
Read a and b Read p, t, r
Step 3: [compute its addition] Step 3: [compute simple interest]
Sum a + b si (p*t*r)/100
Step 4: print Sum Step 4: Display si
Step 5: Stop Step 5: Stop
Exercise:
Write an Algorithm for the following:
1. Multiplication, Subtraction of two numbers.
2. Swapping of two numbers:
a. Using temporary variable.
b. Without using temporary variable.
3. Area of Triangle, Rectangle, Square, Circle.
4. Area of Triangle when three sides are given.
5. Circumference of Circle.
6. Perimeter of the Rectangle.
7. Greatest of two numbers.
8. Greatest of three numbers.
9. Number is Even or Odd.
10. Number is Positive or Negative.
11. To find sum and average of 3 numbers.
12. To check the character is Alphabet or Not.
13. The XYZ construction company plans to give a 5% year-end bonus to each of its
employees earning Rs.5000 or more per year, and a fixed bonus of Rs.250 to all
other employees. Draw a flowchart and write a step form algorithm for printing a
bonus of any employee
2 Mr. Rajesh S M, Assistant Professor
Programming with C 2017
Flow charts
A flowchart is a pictorial or graphical representation of an algorithm or a program.
It consist sequences of instructions that are carried out in an algorithm.
The shapes represent operations.
Arrows represent the sequence or flow in which these operations are carried out.
It is mainly used to help the programmer to understand the logic of the program.
Disadvantages of flow chart
Not suitable for large or complex program
Time consuming
Expensive
Difficult to modify
Notations used in flowchart
Shape Name Meaning
Start and Stop Used to denote start and stop of flowchart
Parallelogram used to read input and display
Input and Output
output
Rectangle used to include instructions or
Processing
processing statements
To include decision making statements rhombus
Decision/ Condition
is used
Connector Join different flow chart or flow lines
Repetition/ Loop Hexagon is used for looping constructs
To include functions in a program, the name of
Functions
function should be enclosed within the figure
Control Flow Lines Arrows denoting flow of control in flow chart
For including the comments, definition or
Comment box
explanation
3 Mr. Rajesh S M, Assistant Professor
Programming with C 2017
Ex 1. Algorithm and Flowchart to Input the dimensions of a rectangle and print area and perimeter
Algorithm: Flowchart:
Step 1: Start
Step 2: Read length, breadth
Step 3: area length * breadth
Step 4: Compute ‘perimeter’
Step 5: Perimeter2*(length+breadth)
Step 6: Display area, perimeter
Step 7: stop
Ex 2. Algorithm and Flowchart to Input 3 numbers and print the biggest number.
Algorithm: Flowchart:
Step 1: start
Step 2: input a,b,c
Step 3: if a>b
Bigaba
else
bigabb
end if
Step 4: if c>bigab
display c
else
display bigab
end if
Step 5: stop
4 Mr. Rajesh S M, Assistant Professor
Programming with C 2017
Exercise:
Write an Flowchart for the following:
1. Addition, Multiplication, Subtraction of two numbers.
2. Swapping of two numbers:
a. Using temporary variable.
b. Without using temporary variable.
3. Area of Triangle, Rectangle, Square, Circle.
4. Calculate Simple Interest.
5. Area of Triangle when three sides are given.
6. Circumference of Circle.
7. Perimeter of the Rectangle.
8. Greatest of two numbers.
9. Number is Even or Odd.
10. Number is Positive or Negative.
11. To find sum and average of 3 numbers.
12. To check the character is Alphabet or Not.
13. The XYZ construction company plans to give a 5% year-end bonus to each of its
employees earning Rs.5000 or more per year, and a fixed bonus of Rs.250 to all
other employees. Draw a flowchart and write a step form algorithm for printing a
bonus of any employee
5 Mr. Rajesh S M, Assistant Professor
Programming with C 2017
Note:
1. Need for writing algorithm and flow charts
Effective communication
Effective analysis
Effective coding
Effective debugging
Easy maintenance
Proper documentation
2. Pseudocode: A solution to Problem
It is the first step in writing a program.
It is written using a mixture of English and C language.
It is a series of steps to solve a given problem
It acts as a problem solving tool
Ex 1: Addition of two numbers Ex 2: Area of Circle
Get the numbers [a,b] Get the radius[r]
Compute addition[Sum= a + b] Compute area[Area = 3.141*r*r]
Print the results [Sum] Print the results [Area]
Advantage:
Easy to write and understand
It is relatively easy to convert English description solution of small programs to C program.
Disadvantage:
It is very difficult to translate the solution of lengthy and complex problem in English to C
6 Mr. Rajesh S M, Assistant Professor