Basics of Programming Lecture 02
Basics of Programming Lecture 02
Lecture 02
Unit 2: Apply Flowchart Diagrams in Constructing Algorithms for
Computer Program Solutions
• Definition of a flowchart
• Common flowchart symbols and their meanings
• Identifying action flows for a given problem
• Constructing flowchart diagrams to represent problem-solving processes
• Limitations of using flowcharts in programming solutions
Definition of Algorithm
An algorithm is a well-defined sequence of steps or rules
designed to solve a problem or perform a particular task.
Algorithms are a foundational concept in computer science
and mathematics, providing a structured approach to
problem-solving. A good algorithm is usually efficient, clear,
and able to solve the problem with minimal computational
resources.
Design/planning a solution
Example 1:
Problem: Students in a class plan to go on a picnic and decide to share the
expenses among them.
Solution: Calculate the total expenses and the amount an individual has to give for
a picnic. This is a kind of problem-solving.
Example 2:
Problem: You’re watching a news channel on your TV and you want to change it to
a sports channel
Solution: you need to move to that channel by pressing that channel number on
your remote. This is a kind of problem-solving.
The above we can broadly say that a problem is a kind of barrier to achieving
something and problem solving is the process of getting that barrier removed by
performing some sequence of activities.
Algorithms – Properties/Characteristics
A well-constructed algorithm should have the following characteristics:
• Finiteness: The algorithm must always terminate after a finite number of
steps.
• Definiteness: Each step must be precisely defined; the actions to be
carried out must be rigorously and unambiguously specified for each case.
• Input: An algorithm has zero or more inputs, taken from a specified set of
objects.
• Output: An algorithm has one or more outputs, which have a specified
relation to the inputs. The output values are the solution to the problem.
• Effectiveness: All operations to be performed must be sufficiently basic
that they can be done exactly and in finite length.
Symbols for writing algorithm
• Symbols for different operations while writing algorithms:
• ‘+’ for Addition
• ‘-’ for Subtraction
• ‘*’ for Multiplication
• ‘/’ for Division
• ‘🡪 ’ for assignment.
For example, A 🡨 X*3 means A will have a value of X*3.
Algorithms - Examples
Problem 01: Find and display the area of a circle with radius r that is read
from the keyboard.
• Inputs to the algorithm: Radius r of the Circle.
• Expected output: Area of the Circle
Algorithm:
• Step 1: Start
• Step 2: Read\input the radius r of the circle
• Step 3: Area 🡨 PI*r*r // calculation of area
• Step 4: Print Area
• Step 5: Stop/End
Algorithms - Types
Algorithms and flowcharts can be classified into three types of control
structures.
• Sequence
• Branching (Selection)
• Loop (Repetition)
Sequence
A sequence of statements is placed one after the other above
or before another gets executed first. In flowcharts, this
sequence of statements is usually contained in the rectangular
process box.
Sequence Algorithm Example
Problem 02: Find and display the area of a circle with radius r that is read
from the keyboard.
• Inputs to the algorithm: Radius r of the Circle.
• Expected output: Area of the Circle
Algorithm:
• Step 1: Start
• Step 2: Read\input the radius r of the circle
• Step 3: Area 🡨 PI*r*r // calculation of area
• Step 4: Print Area
• Step 5: Stop/End
Branch
The branch refers to a binary decision based on some condition. If the
condition is true, one of the two branches is explored; if the condition
is false, the other alternative is taken.
• In pseudo-codes and programs it is usually represented by the ‘if-
then’ construct.
• In flowcharts, this is represented by the diamond-shaped decision
box.
This structure is also known as the selection structure.
Branching Example:
Problem 03: Write an algorithm to find the greater number between
two numbers
The algorithm:
• Step1: Start
• Step2: Read/input two numbers A and B
• Step3: If A greater than B then C=A
• Step4: if B is greater than A then C=B
• Step5: Print C
• Step6: End
Loop
• The loop allows a statement or a sequence of statements to be repeatedly
executed based on some loop condition.
• It is represented by the ‘while’ and ‘for’ constructs in most programming
languages, for unbounded loops and bounded loops respectively.
• (Unbounded loops refer to those whose number of iterations depends on the
eventuality that the termination condition is satisfied; bounded loops refer to
those whose number of iterations is known beforehand.)
• In the flowcharts, a back arrow hints at the presence of a loop.
• A trip around the loop is known as iteration.
• You must ensure that the condition for the termination of the looping must be
satisfied after some finite number of iterations to avoid an infinite loop.
• The loop is also known as the repetition structure.
Loop Examples:
Problem 04: Write an algorithm to find and print even numbers between
0 and 99.
Algorithm:
• Step 1. Start
• Step 2. I ← 2
• Step 3. Write I //in standard output
• Step 4. I ← I+2
• Step 5. If (I <=98) then go to line 3
• Step 6. End
Problem 05: Design an algorithm that generates even numbers between
1000 and 2000 and then prints them in the standard output. It should
also print the total sum.
Algorithm:
• Step 1. Start
• Step 2. I ← 1000 and S ← 0
• Step 3. Write I
• Step 4. S ← S + I
• Step 5. I ← I + 2
• Step 6. If (I < 2000) then go to line 3 else go to line 7
• Step 7. Write S
• Step 8. End
Note:
• It is common to find problems that require combining the use of these
control structures, for example, a loop within a loop (nested loops), a
branch within another branch (nested if), a branch within a loop, a loop
within a branch, and so forth.
Flowchart steps:
Step 1: Start – Begin the process.
Step 2: Input two numbers (let's say A & B).
Step 3: Add the numbers (A + B)
Step 4: Display the sum
Step 5: End/Stop
Flowchart for Problem 01
Start
Input A and B
A+B
Sum of A+B
Stop
Building algorithm in Flowchart
Problem 02: Finding the largest of two numbers.
Flowchart steps:
Step 1: Start – Begin the process.
Step 2: Input two numbers (let's say A & B).
Step 3: Compare the variables (Decision 1 (Is A > B?))
Step 4: If yes, A is greater.
Step 5: If no, B is the greater.
Step 6: End/Stop – The largest number is displayed, and the
process stops.
Flowchart for Problem 02
Start
Input A and B
Is A No
greater Output B is greater
than B
Yes
Output A is greater
Stop
Problem 03: Check whether a number is even or odd.
Flow of Actions:
1. Start
2. Input a number
3. Check if the number is divisible by 2
4. If true, print "Even number"
5. Else, print "Odd number"
6. End
Flowchart for Problem 03
Start
Enter a positive
number
Is a
number No Output: A number is an
divisible Odd number
by 2
Yes
Output: A number is
an Even number
Stop
Flowchart for Problem 03 (Simplified)
Start
Enter a positive
number (A)
Is A No Output A is an Odd
divisible
number
by 2
Yes
Output: A is an Even
number
Stop
Limitations of Flowcharts
Although flowcharts are useful, they have some drawbacks:
• Complexity: Large problems can make flowcharts too big and hard to
follow.
• Time-consuming: Drawing flowcharts for every program is not always
practical.
• Modification issues: Any small change may require a complete
redesign.
• Not Suitable for All Problems: Some algorithms are better
represented using pseudocode or decision tables.
Class Activity (Algorithm & Flowchart)
Problem 04: Finding the largest of three numbers form the user input.
Flowchart steps:
Step 1: Start – Begin the process.
Step 2: Input three numbers, let's say (A, B and C)
Step 3: Compare the variables (Decision 1 (Is A > B and A > C?))
Step 4: If yes, A is greater. If not, go to the next decision.
Step 5: Compare the variables (Decision 2 (Is B > A and B > C?))
Step 6: If yes, B is the largest.
Step 7: If no, C is the largest.
Step 8: End/Stop – The largest number is displayed, and the process stops.
Flowchart for Problem 04 (Class Activity)
Start
Enter three
no.s A, B &C
Is A > B No Is B > A No
C is greater
&A>C &B>C
Yes Yes
A is Greater B is Greater
Stop
Exercise 01 (Thursday 14:00-1500)
Exercise: ATM Withdrawal Process
• Design an algorithm and flowchart for an ATM withdrawal process where a
user can withdraw money if they have sufficient balance.
Problem Statement:
• A user inserts their ATM card and enters their PIN.
• If the PIN is correct, the system asks for the withdrawal amount.
• If the amount is less than or equal to the account balance, the transaction is
successful, and cash is dispensed.
• If the amount is greater than the account balance, the system displays an
error message.
• If the PIN is incorrect, the system allows up to three attempts before blocking
the card.