Algorithms and Flowcharts-2
Algorithms and Flowcharts-2
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.
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.
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
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.
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
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.
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
Start/Stop
Process
Input/Output
Decision/Branching
Connector
Flow
Manual Input
Predefined Process
SEQUENCE
SELECTION
ITERATION
SEQUENCE
Sequence logic is used for performing instructions one after another in sequence.
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 Flowchart
Iteration Flowchart
ITERATION
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
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
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
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
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
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
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
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
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
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
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
Flowchart No Is
Count > n
Yes
WRITE
Sum
STOP
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
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
Yes
Flowchart
WRITE
Sum
STOP
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
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