Algorithms & Folwcharts
Algorithms & Folwcharts
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.
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
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
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
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
Note:
1. Need for writing algorithm and flow charts
Effective communication
Effective analysis
Effective coding
Effective debugging
Easy maintenance
Proper documentation
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