Algorithms and Flowcharts
Algorithms and Flowcharts
Programming
• Programming task can be divided into two phases
Problem solving phase: ordered sequence of steps describing the
solution to the problem called algorithm
Implementation phase: Implement the program in some
programming language
2
Steps in Problem Solving
• Pseudocode: Artificial and informal language that helps programmers
develop algorithms to develop a particular problem. It is very similar
to how we write English Statements.
• Flowchart: A flowchart is a diagram using different shapes (for
different purposes) and arrows in order to demonstrate workflow, or
process, or an algorithm.
Shows logic of an algorithm
Emphasizes individual steps and their interconnection
3
Flowchart
Shapes used in a flowchart:
Start/Stop Input/Output
Process Condition
Flow line 4
Problems
• Write a pseudocode and flowchart to determine a student’s final grade
(average of four examinations), and determine whether he passed or failed.
• Write a pseudocode and flowchart to compute the maximum among three
different numbers
• Write a pseudocode and flowchart to convert a length given in feet to
centimeter. (1 feet = 30.48 cm)
• Write a pseudocode and flowchart for displaying all the roots of quadratic
equation ax2+bx+c=0. (Consider negative discriminant case as well)
• Write a pseudocode and flowchart to read two sides of a rectangle and
compute its area
• Write a pseudocode and flowchart to display if an input number is even or
odd
5
Solved Examples
Algorithm to Display if an Input Number is Even or Odd
1. Start
2. Input a number N
3. If N%2 is equal to zero
3.1 Display “The number is Even”
3.2 Display “The number is Odd”
4. End If
5. Stop
Flowchart to Display if an Input Number is Even or Odd
Start
Input a number N
True False
N%2 = 0?
Display Number is
Even Display Number is Odd
Stop
Algorithm to Display Roots of a Quadratic Equation
1. Start
2. Input coefficients a,b,c
3. Declare variables D, r1, r2
4. Compute D b*b – 4*a*c
5. If D is greater than or equal to 0
5.1 r1 (-b+√D)/(2*a)
5.2 r2 (-b-√D)/(2*a)
5.3 Else
5.4 r1 (-b+j√-D)/(2*a)
5.5 r2 (-b-j√-D)/(2*a)
5.6 End If
6. Display r1 and r2
7. Stop
Flowchart to Display Roots of a Quadratic Equation
Start
Let the quadratic equation be ax2+bx+c=0
Input Coefficients
a,b,c
False D>=0?
True
r1 = (-b+j√-D)/(2*a) r1 = (-b+√D)/(2*a)
r2 = (-b-j√-D)/(2*a) r2 = (-b-√D)/(2*a)
Display r1 and r2
Stop
Algorithm to Display the Maximum Among Three Different
Numbers
1. Start
2. Input three numbers A,B, and C
3. If A is greater than B
4. If A is greater than C
5. Display “Maximum is A”
6. Else
7. Display “Maximum is C”
8. End If
9. Else
10. If B is greater than C
11. Display “Maximum is B”
12. Else
13. Display “Maximum is C”
14. End If
15.End If
16.Stop
Flowchart to Display the Maximum Among Three Different Numbers
Start
True False
A>B?
Stop