Algorithm and Flowchart
Algorithm
A step-by-step solution for
specific problem
Why learn Algorithm?
❖ To understand the core idea behind a problem.
❖ To understand the flow of the problem.
❖ To give the designer a clear explanation of the needs and goal of the issue.
❖ To find a solution for the problem.
❖ To provide a straightforward explanation of how it works without getting into
too much technical detail.
❖ To analyze and evaluate the complexity (in terms of time and space) of a
problem.
Algorithm
❖ An algorithm is a set of well-defined steps or instructions used to solve
specific problems or perform a particular task.
❖ Writing algorithms involves breaking down a complex problem into smaller
and more manageable steps. Usually, one of the following conventions is
used while writing algorithms:
○ Written descriptions.
○ code.
❖ For an algorithm, you need to specify:
1) Input
2) Process
3) Output
Algorithm
An algorithm is a step-by-step set of instructions for performing a task.
Step Algorithm
1 START Inputs
2 Read first number
3 Read second number
Process
4 Add first and second
number
Output
5 Display the sum
6 End
Demo 1 - Brushing Your Teeth
Step Algorithm
1 START
2 Walk to a bathroom.
3 Put toothpaste on the toothbrush.
4 Brush your teeth.
5 Rinse your mouth and toothbrush.
6 Put the toothbrush away.
7 End
Activity 1 - Writing Algorithm
1. Divide yourselves into four groups.
2. Each group will receive a topic and in respective groups
discuss the steps.
3. Write an algorithm for the problem in your notebook.
4. Then share the algorithm to the class.
1 2 C
3 4
Crossing a road Preparing TEA Cooking Washing hands
Activity 1 - Solution
1 2
Algorithm for Crossing a Algorithm for Preparing
road Tea
1. Walk to an edge of a road. 1. Add tea leaves and water
in a pot.
2. Look right and left for a
vehicle. 2. Boil for 15 minutes.
3. Wait if there is a vehicle. 3. Takeout the tea leaves.
4. Cross the road once it is 4. Add salt and butter.
safe.
5. Mix with a machine.
5. Reach the other side.
6. Ready to drink.
Activity 1 - Solution
3 4
Algorithm for Preparing Algorithm for Washing
ema-datshi hands
1. Wash the chillies, onions and 1. Wet hands and apply soap.
tomatoes. 2. Rub the palms together.
2. Chop and put in a pot. 3. Rub the back of hands.
3. Add oil, salt and cheese. 4. Rub the back of the fingers.
4. Cook for 20 minutes. 5. Rub and clean the thumbs.
5. Stir for few seconds. 6. Rub and clean the tips of the
6. Ready to eat. fingers.
7. Rinse the hands.
Characteristics of Algorithm
❖ Clear: The steps in the algorithm should be easy to understand.
❖ Inputs: The algorithm should have one or more inputs.
❖ Outputs: The algorithm should have one or more outputs.
❖ Effective: The algorithm should produce the correct output for the given
input.
❖ General: The algorithm should be able to work with different inputs and
produce the correct output.
❖ Independent: The algorithm should be independent of any programming
language.
ALGORITHM
ALGORITHM
Simple loop
Branch
Example 1
Problem: Write an algorithm to multiply any two numbers and print the result.
STEP Algorithm
1 START
2 Take input for x and y variable from the user.
3 Multiply x and y variables using * operator and assign the result to z
4 Print the variable Z
5 End
Example 2
❖ Design an algorithm to add any two numbers and display the result.
STEP Algorithm
1 START
2 Read any two numbers x , y
3 addition = x + y
4 Print addition
5 End
Example 3
Problem: Write an algorithm to find the greatest of two numbers.
STEP Algorithm
1 START
2 Read any two numbers a, b
3 Check if a>b:
4 Print “a is greater than b”
5 Else:
6 Print “b is greater than a”
7 End
Activities
Activity 1: Write an algorithm that adds two numbers.
Activity 2: Write an algorithm to find the area of the circle.
Activity 3: Write an algorithm to calculate the perimeter of a rectangle.
Activity 4: Write an algorithm for finding the largest of any three given numbers.
Activity 5: Write an algorithm that prompts the user to input their exam marks
and determines whether they have passed or failed. The pass mark is 40 out of
100.
Answers
Activity 1: Write an algorithm that adds two numbers.
STEP Algorithm
1 START
2 Read any two numbers a , b
3 addition = a + b
4 Print addition
5 End
Answers 2
Activity 2: Write an algorithm to find the area of the circle.
STEP Algorithm
1 START
2 Read a radius of circle (r)
3 area = PI*r**2
4 Print area
5 End
Answers 3
Activity 3: Write an algorithm to calculate the perimeter of a rectangle.
STEP Algorithm
1 START
2 Read a length and width [l, w]
3 perimeter = 2*(L+W) OR perimeter = l+w+l+w
4 Print perimeter
5 End
Answers 4
Activity 4: Write an algorithm for finding the largest of any three given
STEP
numbers. Algorithm
1 START
2 Read any three numbers x, y and z
3 Check if x>y and x>z:
4 Print “x is greater than y and z”
5 Check if y>x and y>z:
6 Print “y is greater than x and z”
7 else:
8 Print “z is greater than x and y”
9 End
Answers 5
Activity 5: Write an algorithm that prompts the user to input their exam marks
and determines whether they have passed or failed. The pass mark is 40 out of
100.
STEP Algorithm
1 START
2 Enter any mark [m]
3 Check if m>=40:
4 Print ”Passed”
5 else:
6 Print “Failed”
7 End
Flowchart
Diagrammatic representation of
an algorithm
Flowchart
❖ A flowchart is a diagrammatic representation of an algorithm. It is a way of
visualizing the steps involved in a process or task.
❖ A flowcharts can be helpful for both writing programs and explaining
programs to others.
❖ Some of the most common symbols used to draw flowcharts are:
◆ Start/End: The START and End of a flowchart can be represented by the
oval shape.
◆ Process: The process of a flowchart can be represented by the rectangle
shape.
◆ Decision: The Decision of a flowchart can be represented by the diamond
shape
◆ Input/Output: The input/output of a flowchart can be represented by the
Symbols
Example 1
Draw a flowchart to find the addition of any two numbers.
STEP Algorithm START
1 START
Read any two numbers x,
2 Read any two numbers x, y y
3 addition = x + y
addition = x + y
4 Print addition
5 STOP/End Print addition
End
Example 2
Draw a flowchart to calculate the perimeter of a Square with side length of 5 units.
1. Flowchart
Algorithm
Step 1: START
Step 2: Store the value of a side in a variable named
side
Step 3: Calculate and store the value of perimeter in
the variable named perimeter
Step 4: Display the perimeter
Step 5: END
Example 3
Draw a flowchart to check whether the entered age is eligible for casting the vote.
STEP Algorithm
1 START
2 Enter an age:
3 Check if age>=18:
4 Print “Eligible to cast vote”
5 else:
6 Print “Not eligible to cast
vote”
7 End
Activities
Activity 1: Draw a flowchart to find multiplication any numbers entered by the user.
Activity 2: Draw a flowchart to find the area of the circle.
Activity 3: Draw a flowchart to calculate the perimeter of the rectangle.
Activity 4: Draw a flowchart to find the largest of any three numbers when a user
reads numbers from the keyboard.
Activity 5: Draw a flowchart that prompts the user to input their ICT exam mark
and determines whether they have passed or failed. The pass mark is 40 out of 100.
Answer 1
Activity 1: Draw a flowchart to find multiplication any numbers entered by the
user. START
Read any two numbers x,
y
multiplication = x * y
Print
multiplication
End
Answer 2
Activity 2: Draw a flowchart to find the area of the circle.
START
Read radius of circle [r]
area = PI*r**2
Print area
End
Answer 3
Activity 3: Draw a flowchart to calculate the perimeter of the rectangle.
START
Read a length and width [l,
w]
perimeter = 2*(l+w)
Print perimeter
End
Answer 4
Activity 4: Draw a flowchart to find the largest of any three numbers when a user reads numbers
from the keyboard.
START
Read any three numbers
x,y,z
Check Yes
x>y and Print “x is greater than y
x>z and z”
No
Check Yes End
Print “y is greater than x
y>x and and z”
y>z
No
Print “z is greater than x
and y”
Answer 4
Activity 4: Draw a flowchart to find the largest of any three numbers when a user reads numbers
from the keyboard.
End
Print “x is greater than y
and z”
Check yes
Read any three numbers
x>y and x>z x,y,z
No
Start
Check yes Print “z is greater than x
y>x and y>z and y”
No
Print “y is greater than x
and z”
Answer 5
Activity 5: Draw the flowchart that prompts the user to input their ICT exam
mark and determines whether they have passed or failed. The pass mark is 40
START
out of 100.
Enter a mark [m]
yes No
Check
m>=40
Print
Print “Failed”
“Passed”
End