We are on a mission to address the digital
skills gap for 10 Million+ young professionals,
train and empower them to forge a career
path into future tech
Programming Fundamentals:
Algorithm
Contents
• Meaning of Algorithm
• Properties or Characteristics
• Building Blocks
3 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Please download pictures in
suitable size here and insert them
by clicking the symbol above.
4 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
What is Algorithm?
• Algorithm is a set of steps to accomplish a desired task.
Example: To
Step
walk out the door, Step Step
Hari does the
Step
following set of Step
actions.
5 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Example: Get the Cake
You are playing a video game where you must
The algorithm is as follows
make the girl get the cake.
• Step 1: Move Right
• Step 2: Jump
• Step 3: Move Right
• Step 4: Move Right
• Step 5: Pick the Cake
Write an algorithm using the following
direction keys Move
Jump Pick
right
6 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Example: Finding Shortest Distance Between Points on Google Maps
Algorithm
Step 1: Open Google Maps on your computer.
Step 2: Zoom into your starting point and right click on it.
Step 3: Select Measure distance from the right-click options.
Step 4: Click on the second location you want to measure the distance
too.
Step 5: If you want to measure multiple points, click again on those
locations. Drag a point or path to adjust it or click on a point to remove it.
7 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Activity
Write an algorithm for the following day today life problem statements:
➢ Make a mug of hot coffee
➢ Book movie tickets in online
➢ Find full length movies on YouTube
➢ To send an Email
➢ About deciding what to wear in the event of rain
8 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
What is Algorithm?
• In Computer Science, an algorithm is a set of well defined
steps for a computer program to accomplish a task.
Start with input data
• When there are multiple algorithms for a particular problem
(and there often are!), the best algorithm is typically the one Do Calculations
that solves it the fastest.
Stop when we find the answer
• It takes some value or a set of values as input and
produces some value or a set of values as output.
9 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Properties or Characteristics
• Finiteness: Algorithm must complete after a finite number of steps.
• Definiteness: Each step must be clearly defined, having one and only one interpretation. At each
point in computation, one should be able to tell exactly what happens next.
• Sequence: Each step must have a unique defined preceding and succeeding step. The first step
(start step) and last step (halt step) must be clearly noted.
• Feasibility: It must be possible to perform each instruction.
• Correctness: It must compute correct answer for all possible legal inputs.
10 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Properties or Characteristics
• Language Independence: It must not depend on any one programming language.
• Completeness: It must solve the problem completely.
• Effectiveness: It must be possible to perform each step exactly and in a finite amount of time.
• Efficiency: It must solve with the least amount of computational resources such as time and space.
• Generality: Algorithm should be valid on all possible inputs.
• Input/Output: There must be a specified number of input values, and one or more result values.
11 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks
• An algorithm is made up of three basic building blocks:
1. Sequencing ( Action): An Action is one or more instructions that the computer performs in sequential
order (from first to last).
Example: Compute 17 plus 31.
2. Selection (Decision): A Decision is making a choice among several actions.
Example: If DAY is SUNDAY, print "HOLIDAY", otherwise print "WORKING DAY“
3. Iteration (Repetition or Loop): A Loop is one or more instructions that the computer performs repeatedly.
Example: Print "HELLO WORLD!" 77 times.
12 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks
Logic of the Problem
Algorithm = Logic + Control
Sequence Selection Looping
13 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Sequencing (Action)
• This describes a sequence of actions that a program carries out one after another, unconditionally.
• Execute a list of statements in order.
Example: Morning Workout
Step 1: Wakeup Step 2: Go to Gym Step 3: Do Workout Step 4: Return Home
14 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Sequencing (Action)
Example 1: Greet a user with the Welcome Message “Welcome<<username>>”
Algorithm
Step 1 : Start
Assume,
username=XYZ
Step 2 : Input username
Welcome XYZ
Step 3 : Print Welcome username
Step 4 : Stop
15 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Sequencing (Action)
Example 2: Find the area of the rectangle
Algorithm
Area of Rectangle = l x w
Step 1 : Start
Here
Step 2 : Input the length of the rectangle l length
w width
Step 3 : Input the width of the rectangle
Step 4 : Find the area of the rectangle as
Area=length*width
Step 5 : Print the area of the rectangle
Step 6 : Stop
16 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Sequencing (Action)
Example 3: Get kilometer from the user and convert it to meter
Algorithm
Step 1 : Start
Meter = km * 1000
Step 2 : Input kilo meter
Here
Step 3 : Convert kilo meter to meter as Km Kilo meter
Meter=kilo meter*1000
Step 4 : Print meter
Step 5 : Stop
17 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Selection (Decision)
• Algorithms can use selection to determine a different set of steps to execute based on a Boolean
expression.
• If the conditional test is true, one part of the algorithm will be executed, otherwise it will execute the
other part of the algorithm
Example: To decide Sunday play option Step 4 : Play
Yes Cricket
No
Step 4 : Play Video
Step 1 :Morning Step 2 :Call Friends Step 3 : if Friends Game
Wakeup available?
18 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Selection (Decision)
Example 1: Find the smallest of given two numbers
Algorithm I/O
A=20 B=40
Step 1 : Start
Print A is smallest number
Step 2 : Read values of two numbers, A and B
Step 3 : Check whether A is less than B, if the condition is true then goto step 3.1 else goto step 3.2
Step 3.1:print A is the smallest number
Step 3.2: print B is the smallest number
Step 4 : Stop
19 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Selection (Decision)
Example 2: Check whether the given number is three-digit number or not.
Algorithm Assume,
A=150
Step 1 : Start
If A>99 and A<1000 (True)
Step 2 : Read the number, A The number is a three digit number
Step 3 : Check whether the given number is greater than 99 and less than 1000, if yes then go to step 3.1
otherwise go to step 3.2
Step 3.1: Print the message “The number is a three digit number”, go to step 4
Step 3.2: Print the message “The number is not a three digit number”
Step 4 : Stop
20 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Selection (Decision)
Example 3: Check whether the given year is Leap Year or not. year=2100
Algorithm It is not a leap year
Step 1 : Start
Step 2 : Read the value of the year
Step 3: Check whether the given year is divisible by 4 and not divisible by 100, if yes goto 3.1 otherwise
goto 3.2
Step 3.1: print the message “It is Leap year”, goto step 4
Step 3.2: Check whether the given year is divisible by 400, if yes goto 3.2.1
otherwise goto 3.2.2
Step 3.2.1: print the message “It is Leap year”
Step 3.2.2: print the message “It is not Leap year”
Step 4 : Stop
21 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Iteration (Repetition or Loop)
• Algorithms often use repetition to execute steps for a certain number of times or until a certain condition
is met.
• Example: Our daily routine as a student
Step 2: Brush your teeth. Step 3: Take a shower.
Step 1: Wake-up Step 4: Dress-up.
Step 7: Repeat the
process as a daily Step 6: Drive to the work/ Step 5: Get into the car/
routine ride to the college. take your bike.
22 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Iteration (Repetition or Loop)
Example 1: Print “Hello World” 5 times O/P
Hello World
Algorithm
Hello World
Step 1 : Start Hello World
Hello World
Step 2 : Initialize the value of i as 1 Hello World
Step 3 : Check the condition i less than or equal to 5, if the condition is true goto step 3.1 else goto step 4
Step 3.1: Print “Hello World” and increment the value of i by 1
Step 3.2: Repeat the Step 3 until the condition is true
Step 4 : Stop
23 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Iteration (Repetition or Loop)
Example 2: Print first N natural numbers Assume N = 5,
Algorithm O/P
1
Step 1 : Start 2
3
Step 2 : Initialize the value of i as 1 4
5
Step 3: Read the value of a number, N
Step 4 : Check the condition i less than or equal to N, if the condition is true goto step 4.1 else goto step 5
Step 4.1: Print i and increment the value of i by 1
Step 4.2: Repeat the Step 4 until the condition is true
Step 5 : Stop
24 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Iteration (Repetition or Loop)
Example 3: Print Sum of first N natural numbers
Assume N = 5,
Algorithm (1+2+3+4+5)
Step 1 : Start
O/P
Step 2 : Initialize the value of i as 1 and sum as 0 15
Step 3: Read the value of a number, N
Step 4 : Check the condition i less than or equal to N, if the condition is true goto step 4.1 else
goto step 5
Step 4.1: calculate sum=sum+i
Step 4.2: Increment the value of i by 1 and repeat the Step 4 until the condition is true
Step 5 : print sum
Step 6 : Stop
25 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Arrays
• An array is a container that holds a fixed number of values of a same data type.
• Need of Array: Difficult to manage large number of variable in normal way. The idea of array is to
represent many instances in one variable.
• Length of the array is fixed and index starts with 0. Can’t access the elements beyond the array
limit.
26 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Arrays
Example 1: Get 5 numbers from the user and print the numbers using array
Algorithm
num[5] = {1,2,3,4,5}
Step 1: Start
Step 2: Initialize an empty array num of size 5. O/P
[1, 2, 3, 4, 5]
Step 3: Initialize a variable i to 0.
Step 4: Repeat steps 5-7, until i is less than 5.
Step 5: Prompt the user to input a number and store it in num[i].
Step 6: Increment i by 1.
Step 7: End of the loop.
Step 8: Print "The numbers entered by the user are:".
Step 9: Initialize a variable j to 0.
27 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Arrays
Step 10: Repeat steps 11-13, until j is less than 5.
Step 11: Print num[j].
Step 12: Increment j by 1.
Step 13: End of the loop.
Step 14: Stop
28 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Arrays
Example 2: sum of 'n' numbers using array
n=5
Algorithm
num[n]={1,2,3,4,5}
Step 1: Start
Step 2: Prompt the user to input the value of n. O/P
15
Step 3: Initialize an empty array num of size n.
Step 4: Initialize a variable sum to 0.
Step 5: Initialize a variable i to 0.
Step 6: Repeat steps 7-9, until i is less than n.
Step 7: Prompt the user to input a number and store it in nums[i].
Step 8: Add nums[i] to sum.
Step 9: Increment i by 1.
29 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Arrays
Step 10: End of the loop.
Step 11: Print "The sum of the numbers entered by the user is: sum".
Step 12: Stop
30 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Function (Subroutine)
• Function is a sub task which consists of block of instructions that performs a particular task.
• For complex problems, the problem is been divided into smaller and simpler tasks during algorithm
design.
Instruction
Call Subroutine Instruction Subroutine
Next Instruction
31 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Uses of Function
• A function/Method is a block of code which perform specific task and the task is executed when the
function is invoked or called.
Primary uses of functions:
– It allows code reusability (define once and use multiple times)
– A complex program can be modularised into smaller modules/functions for better readability
and understanding,
– Reduces duplication of code by facilitating multiple calls to the same function/module
32 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Function (Subroutine)
Example 1: Addition of two numbers
Algorithm In Main Function
Main Function() Call Sub Function add()
Step 1 : Start
In Function add()
Step 2 : Call the function add() Read a & b values
c=a+b
Step 3 : Stop
print c
Algorithm Return control back to main
Sub Function add() function
Step 1 : Function start
Step 2 : Get a, b Values
Step 3 : add c=a+b
Step 4: Print c
Step 5: Return
33 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Building blocks: Function (Subroutine)
Example 2: Find area of a circle
Algorithm
Main Function() In Main Function
Step 1 : Start Read Radius value
Step 2 : Read radius Call Sub Function area_of_circle()
Step 3 : Call the function area_of_circle and and pass radius value
pass radius
Step 4: Print the area In Function area_of_circle()
Step 3 : Stop Calculate area of circle
Return area value to main function
Algorithm
Sub Function area_of_circle(radius)
Step 1 : Function start
Step 2 : Calculate area as
area=3.14*(radius*radius)
Step 3: Return area
34 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Representation
• Almost every program involves the steps of input, processing, and output.
• An algorithm can be written or described or represented using Flow Chart and Pseudocode
Flow chart
– Use standardized symbol to show the steps the computer needs to take to accomplish the
program’s objective.
– Because flowcharts are difficult to revise, they have fallen out of favor by professional
programmers.
35 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Representation
Pseudo code
– Use English‐like phrases to describe the processing process. It is not standardized since every
programmer has his or her own way of planning the algorithm.
Why Algorithm is Important?
• Describe the steps needed to perform a computation
• Important for writing efficient code
- code that execute faster & which uses less memory
36 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Summary
• We have a step-by-step process to do everything in our day-today life.
• In the same way, an algorithm is a set of clear, logical steps to solve a problem or achieve a goal.
• Think of it as a plan that guides you through what needs to be done, just like following the steps in a
recipe.
• Each step in this algorithm tells you exactly what to do, just like a set of instructions.
• Algorithms can be more complex for computer tasks, but the basic idea is always about breaking things
down into simple steps to solve a problem or complete a task.
37 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Quiz
1) Algorithm should be?
a) Precise
b) Unambiguous
c) Clear
d) All of these
Answer : Option d)
38 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Quiz
2) Which of the following statement is correct?
a) Sequence is random execution of a process
b) Sequence is a step-by-step execution of a
process
c) Sequence statements executes in ordered
manner
d) Both b) and c)
Answer : Option d)
39 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Quiz
3) An algorithm is the sequence of instructions or a set of
rules to get something done.
a) True
d) False
Answer : Option a)
40 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
Algorithm
Quiz
4)_____ and ____ statements are used to change the sequence
of execution of instructions.
a) Selection
b) Iteration
c) Function
d) Both a) and b)
Answer : Option d)
41 Programming Fundamentals: Algorithm | © SmartCliff | Internal | Version 1.0
THANK YOU