Algorithms and Flowcharts
Problem Analysis
Problem analysis can be defined as studying a problem to arrive at a satisfactory solution. To solve a problem
successfully, the first step is to understand the problem. Also the problem must be stated clearly and accurately
without any confuse. A well-defined problem and clear description of input and output are important for an
effective and efficient solution. Study the outputs to be generated so that input can be specified. Design the
steps, which will produce the desired result after supplying the input.
If the problem is very complex, split the problem into multiple sub-problems. Solve each sub-problem and
combine the solutions of all the sub-problems to at arrive at overall solution to a large problem. This is called
divide and conquer technique.
Problem solving steps
o Understand the problem and plan its logic
o Construction of the List of Variables
o Develop an algorithm
o Refine the algorithm. Refining means making changes
o Program Development
o Testing the Program (solution)
o Validating the Program (solution)
Algorithm
An algorithm is defined as sequence of steps to solve a problem (task). The steps must be finite, well defined
and unambiguous. Writing algorithm requires some thinking. Algorithm can also be defined as a plan to solve a
problem and represents its logic. Note that an algorithm is of no use if it does not help us arrive at the desired
solution
Algorithm characteristics
1. It should have finite number of steps. No one can be expected to execute infinite number of steps.
2. The steps must be in order and simple
3. Each step should be defined clearly stated i.e. without un-ambiguity
4. Must include all required information
5. Should exhibit at least one output
For accomplishing a particular task, different algorithms can be written. Different algorithms can differ in their
requirements of time and space. Programmer selects the best suited algorithm for the given task to be solved.
1|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
Algorithm for preparing two cups of tea
1. Add 1.5 cups of water to the vessel
2. Boil water
3. Add 2 tea spoons of tea leaves
4. Add half cup of milk
5. Add some sugar
Statement 5 is an example of an ambiguous (unclear) statement. This statement doesn’t clearly state the
amount of sugar to be added.
Algorithm characteristics
1. It should have finite number of steps. No one can be expected to execute infinite number of steps.
2. The steps must be in order and simple
3. Each step should be defined clearly stated i.e. without un-ambiguity (without doubtfulness)
4. Must include all required information
5. Should exhibit at least one output
Algorithm Flowchart Program
An algorithm is defined as A flowchart is pictorial Set of instructions. Instruction is
sequence of steps to solve a (graphical) representation of a command to the computer to
problem (task). an algorithm. do some task.
Algorithm can also be defined as A picture is worth of 1000 Implementation of Algorithm or
a plan to solve a problem and words. We can understand more flowchart
represents its logic. from picture than words.
Different algorithms have different performance characteristics to solve the same problem. Some
algorithms are fast. Some are slow. Some occupy more memory space. Some occupy less memory
space. Some are complex and some algorithms are simple.
Logically algorithm, flowchart and program are the same.
Algorithm design
▪ Design an algorithm that is easy to understand code and debug. Debugging is the process finding and
fixing errors in a program
▪ Design an algorithm that makes use of resource such as space (memory) and time efficiently
2|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
Flowchart
A flowchart is a pictorial (graphical) representation of an algorithm. A flowchart is drawn using different
kinds of symbols. A symbol is used for a specific purpose. Each symbol has name.
Flowcharts use different shapes of boxes to denote different type of instructions. ANSI recommended a number
of different rules and guidelines to help standardize the flowcharting process.
Algorithms are represented using flowcharts
Flowchart symbols are standardized by ANSI
Flowchart helps to divide a large complex problem into small manageable ones
Generally, algorithm is first represented as a flowchart and then expressed in a programming
language
While preparing a flowchart, the sequence, selection and iterative structures may be used wherever
required
Note
Experienced programmers, sometimes write programs without drawing a flowchart. Beginners should first draw
a flowchart to reduce number of errors in the program.
Rules for Drawing a Flowchart
It should contain only one start and one end symbol
The relevant symbols must be used while drawing a flowchart
The direction of arrows should be top to bottom and left to right
It should be simple and drawn clearly and neatly
Be consistent in using names, variables in the flow chart
Use properly labeled connectors to link the portions of the flowchart on different pages
The branches of decision box must be label
Advantages of Flowcharts
Conveys better meaning
Analyses the problem effectively
Good tool for documentation
Provide guide for coding
Systematic debugging
Systematic testing
Disadvantages of Flowcharts
Takes more time to draw. Imagine developing a detailed flowchart for a program containing 50000
lines or statements of instructions
Difficult to make changes
Non-standardization - No standards to determine amount of details should be included in a flowchart
3|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
Flowchart Symbols
Symbol Meaning
Start/Stop
Process
Input/Output
Decision/Branching
Connector
Flow
Manual Input
Predefined Process
4|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
PROGRAMMING CONSTRUCTS
There are THREE basic programming constructs. They are:
SEQUENCE
SELECTION
ITERATION
SEQUENCE
Sequence logic is used for performing instructions one after another in sequence.
Sequence is the most basic of the constructs
It is simply performing one step after another
Each step is followed in a specific sequence, hence the name
Sequence can be thought of as “do this, then do this, then do this”
Sequence Flowchart
SELECTION
Selection logic, also known as decision logic, is used for making decisions. Selection logic is depicted as either
an IF...THEN...ELSE or IF.....THEN structure.
Selection is the decision-making construct.
It is used to make yes/no or true/false decisions logically.
Selection can be thought of as “if something is true, take this action, otherwise take that action”.
Selection Flowchart
5|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
ITERATION
Iteration logic is also known as Loop. Iteration logic is used when one or more instructions may be executed
several times depending on some condition.
Iteration Flowchart
ITERATION
Iteration comes from the word “reiterate”, which means to repeat
Iteration is a looping construct
Iteration is a combination of decision and sequence and can repeat steps
Iteration can be thought of as “while something is true, do this, otherwise stop”
6|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
To find sum of two numbers START
Algorithm
READ A, B
1. Start
2. Read A,B
3. C=A+B
C=A+B
4. Print or display C
5. Stop
WRITE
Flowchart C
STOP
Finding Area of the square
START
Algorithm
1. Start READ L
2. Read length, L
3. Area = L*L
4. Print or display Area
Area = L*L
5. Stop
WRITE
Area
STOP
7|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
Finding Area of the rectangle
START
Algorithm
1. Start READ A
2. Read side length, A
3. Read side Length B
4. Area = A*B
READ B
5. Print or display Area
6. Stop
Area = A*B
Flowchart
WRITE
Area
STOP
Interchange the value of two numbers
START
Algorithm
1. Start READ A
2. Read two values into two variables a, b
3. Declare third variable, c
c=a READ B
a=b
b=c
4. Print or display a, b
C=A
5. Stop A=B
B=C
WRITE
Flowchart A, B
STOP
8|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
Calculating the average for 3 numbers
START
Algorithm
READ A , B , C
1. Start
2. Read 3 numbers A, B, C
3. Calculate the average by the equation
Average = (A + B + C)/3 Average = (A + B + C)/3
4. Print average
5. Stop
WRITE
Average
Flowchart
STOP
Greatest of two numbers START
Algorithm
READ A, B
1. Start
2. Read A,B
3. If A>B then
Print A is large Is A>B?
else Y N
Print B is large
4. Stop WRITE WRITE
A is Larger B is Larger
Flowchart
STOP
9|Page Youtube.com/ EngineersTutor www.EngineersTutor.com
Find the area & perimeter of a square
START
Algorithm
1. Start
2. Read length L READ L
3. Area A = L*L
4. Perimeter P = 4*L
5. Print or display A, P A = L*L
6. Stop P = 4*L
WRITE
A, P
STOP
Find the Sum of First Five Natural Numbers
START
Algorithm
1. Start READ n
2. Read n, initialize count = 0, sum = 0
3. count = count + 1 count = 0
4. sum = sum + count sum = 0
5. Repeat steps 3,4 until count>5
6. Print sum
7. Stop count = count+ 1
sum = sum + count
No Is
count > 5
Yes
Flowchart
WRITE
sum
STOP
10 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com
Area of a triangle where three sides are given
Algorithm START
1. Start
2. Read a, b, c READ a, b, c
3. s=(a+b+c)/2
4. A=sqrt (s *(s-a)*(s-b)*(s-c))
5. Print or display A
6. Stop s = (a+b+c)/2
A = sqrt s(s-a)*(s-b)*(s-c)
WRITE
Flowchart A
STOP
Calculate simple interest using the expression (SI=PNR/100)
Algorithm
START
1. Start
2. Read P, N, R
3. SI=(PNR)/100 READ P, N, R
4. Print SI
5. Stop
SI = (P*N*R)/100
Flowchart WRITE
SI
STOP
11 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com
Convert temperature from Fahrenheit to Celsius
START
Algorithm
Initialize F =0, C =0
1. Start
2. Initialize F = 0, C = 0
3. Read F READ F
4. C = (F-32) * 5/9
5. Write C
6. Stop
C = (F-32) * 5/9
Flowchart
WRITE
C
STOP
Calculating sum of integers 1 to 100
START
Algorithm
i=1
sum = 0
1. Start
2. Initialize count i = 1, sum = 0
3. sum = sum + i sum = sum + i
4. Increment i by 1
5. Repeat steps 3 & 4 until i > 100
6. Print sum
7. Stop i=i+1
Flowchart No Is
i>100?
Yes
WRITE
sum
STOP
12 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com
START
Draw a flowchart for computing factorial N
Where N! = 1 * 2 * 3 * …… N
READ N
F=1
i=1
F=F*i
i=i+1 No Is
i = N?
Yes
Flowchart WRITE F
STOP
To find the sum of n natural Numbers
START
Algorithm READ n
8. Start
9. Read n Count = 0
10. Count=0 Sum = 0
11. Sum=0
12. Count=Count+1
13. Sum=Sum + Count Count = Count+
14. Repeat steps 5 & 6 until i
15. Count>n
16. Print sum Sum = sum + count
17. Stop
No Is
Flowchart Count > n
Yes
WRITE
Sum
STOP
13 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com
To find the sum of all even numbers up to ‘n’
START
Algorithm
1. Start READ n
2. Read n
3. Count=0
4. Sum=0 count = 0
5. Count=count+2 Sum = 0
6. Sum=sum + count
7. 7Repeat steps 5 &6 until count>=n
8. Print sum count = count+ 2
9. Stop
Sum = Sum +count
Flowchart No Is
Count > n
Yes
WRITE
Sum
STOP
To find Product of N numbers
START
Algorithm
READ n
1. Start
2. Read n
3. Count=0 count = 0
4. Product=1 product = 1
5. Count=count+1
6. Product=count*product
7. Repeat steps 5,6 until count>N count = count+ 1
Product = Product*count
8. Print Product
9. Stop
Is
count > n
Flowchart
No
Yes
WRITE
sum
STOP
14 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com
Sum of squares of n natural numbers
Algorithm START
1. Start
2. Read n READ n
3. i=0,sum=0
4. i=i+1
5. Sum=sum+(i*i) i=0
6. Repeat steps 4 and 5 until i>n Sum = 0
7. Print sum
8. Stop
i=i+1
Sum = Sum + (i*i)
Flowchart
No Is
i>n
Yes
WRITE
Sum
STOP
Sum of first 50 odd numbers START
Algorithm
Sum = 0
1. Start N=1
2. Sum=0,n=1
3. Sum=sum + n
4. n=n+2 Sum =Sum + N
5. Repeat steps 4 and 5 until n<=99
6. Print sum
7. Stop
N = N+2 No Is
N <= 99?
Flowchart
Yes
WRITE
sum
15 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com
Calculating percentage of marks START
Algorithm
READ
1. Start input data
2. Read Input data
3. Add marks of all subjects giving total
𝑀𝑎𝑟𝑘𝑠 𝑜𝑏𝑡𝑎𝑖𝑛𝑒𝑑 Find Marks
4. Percentage = 𝑇𝑜𝑡𝑎𝑙 𝑚𝑎𝑟𝑘𝑠 ∗ 100 obtained
5. Write Percentage
6. Stop
Find Percentage
Yes
Flowchart
WRITE
Sum
STOP
To find the sum of all even numbers up to ‘n’
START
Algorithm
1. Start READ n
2. Read n
3. Count=0
4. Sum=0
count = 0
5. Count=count+2 sum = 0
6. Sum=sum+count
7. Repeat steps 5 &6 until count>=n
8. Print sum count = count+ 2
9. Stop
sum = sum + count
Flowchart
No Is
count > n
Yes
WRITE
sum
STOP
16 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com