Problem Solving Tools
Problem Solving Tools
Algorithm
Definition ⇒ It is a finite set of instructions which perform a specific task using computational
manner.
Father of algorithm → Mohammad Ibn Musa al-Khwarizmi
Any algorithm must follow 5 criteria
1. Input ⇒ An algorithm should take finite number of inputs.
It is essential for any algorithm before starting.
Input should be given to it initially before the Algorithm begins.
2. Output ⇒ An algorithm must give at least one required result from the given set of input values.
These output values are known as the solution to a problem.
3. Definiteness ⇒ Each step should be unique
No step should be repeated
Each step must be clear and precisely defined.
4. Finiteness ⇒ It means algorithm should be terminated after a finite number of steps.
Also, each step should be finished in a finite amout of time.
5. Correctness ⇒ Output should be correct.
3 Types of Algorithm
1. Sequential
2. Conditional
3. Looping
Algorithm Questions
1. Write an algorithm to calculate sum of two numbers.
step 1 : start
step 2 : input n1, n2
step 3 : sum = n1 + n2
step 4 : print sum
step 5 : stop
2. Write an algorithm to calculate area of rectangle.
step 1 : start
Problem Solving Tools
step 2 : input l, b
step 3 : area = l * b
step 4 : print area
step 5 : stop
3. Write an algorithm to calculate area of circle.
step 1 : start
step 2 : input r
step 3 : area = 3.14 * r * r
step 4 : print area
step 5 : stop
4. Shopkeeper sells mangos @ 10 Rs/mango a customer buys 5 mangoes from shop keeper, write
an algorithm to calculate bill
step 1 : start
step 2 : amount = 10 * 5
step 3 : print amount
step 4 : stop
4. Shopkeeper sells mangos @ 10 Rs/mango, write an algorithm to calculate bill
step 1 : start
step 2 : input n(no. of mangoes)
step 3 : amount = 10 * n
step 4 : print amount
step 5 : stop
Flowchart
Diagramatic representation of a process.
It help us to maintain proper sequence of algorithm.
SYMBOLS Problem Solving Tools
start/stop
input/output
process
decision
flow of process
Sum of two numbers
start Problem Solving Tools
input n1, n2
sum = n1 + n2
print sum
stop
Area of rectangle
start Problem Solving Tools
input l, b
area = l * b
print area
stop
Swapping of two number
Swapping using 3 variables
Algorithm
Step 1 : start
step 2 : input a & b
step 3 : c = a
a=b
b=c
step 4 : print a and b
step 5 : stop
Flowchart
start Problem Solving Tools
input a, b
c=a
a=b
b=c
print a, b
stop
Swapping without using third variable (+, - operator)
Algorithm
Step 1 : start
step 2 : input a & b
step 3 : a = a + b
b=a-b
a=a-b
step 4 : print a and b
step 5 : stop
Flowchart
start Problem Solving Tools
input a, b
a=a+b
b=a-b
a=a-b
print a, b
stop
Swapping without using third variable (*, / operator)
Algorithm
Step 1 : start
step 2 : input a & b
step 3 : a = a * b
b=a/b
a=a/b
step 4 : print a and b
step 5 : stop
Flowchart
start Problem Solving Tools
input a, b
a=a*b
b=a/b
a=a/b
print a, b
stop
Algorithm + Flowchart
Check whether any positive number is single digit or multi digit.
Algorithm
Step 1 : start
Step 2 : input n
Step 3 : if n ≤ 9
print number is single digit
else
print number is multi digit
Step 4 : stop
Flowchart
start Problem Solving Tools
input n
n <= 9 F n is multi digit
n is single digit
stop
Check whether a number is even or odd.
Algorithm
Step 1 : start
Step 2 : input n
Step 3 : if n % 2 == 0
print n is even
else
print n is odd
Step 4 : stop
Flowchart
start Problem Solving Tools
input n
n % 2 == 0 F n is an odd number
n is an even number
stop
Check whether any number is positive, negative or zero
Algorithm
Step 1 : start
Step 2 : input n
Step 3 : if (n < 0)
print n is negative number
else if (n > 0)
print n is positive number
else
print n is zero
Step 4 : stop
Flowchart
start Problem Solving Tools
input n
n<0 T
print n is negative number
T
n>0 print n is positive number
n is zero
stop
(Assignment) Input 4 marks of subject and calculate percentage
and make result according to following
percentage >= 70 → 1st division with honours
60 <= percentage <= 70 → 1st division
50 <= percentage <= 60 → second division
40 <= percentage <= 50 → third division
percentage < 40 → failed
Algorithm
Step 1 : start
Step 2 : input m1, m2, m3 & m4
Problem Solving Tools
Step 3 : percentage = (m1 + m2 + m3 + m4)/4
Step 4 : if (percentage >= 70)
print 1st division with honours
else if (percentage >= 60)
print 1st division
else if (percentage >= 50)
print 2nd division
else if (percentage >= 40)
print 3rd division
else
print failed
Step 5 : stop
Flowchart
start Problem Solving Tools
input m1, m2, m3 & m4
perc = (m1 + m2 + m3 + m4) /4
T
perc >= 70 print 1st division with honours
perc >= 60
T print 1st division
T
perc >= 50 print 2nd division
perc >= 40
T print 3rd division
print failed
t
stop
Problem Solving Tools
Looping
If we want to repeat code of blocks many times we use looping
It helps in reducing complexity.
There are three types of loops
for
while
do while
Mandatory statements in loop are -
Initialization → entry point → Provide initial value
Condition → terminating condition
Updation → updates value to reach terminating condition.
An algorithm is used to reduce space & time complexity.
While Loop
Print first 10 natural numbers
Algorithm
Step 1 : start
Step 2 : n = 1 // initialization
Step 3 : repeat step 4 while (n ≤ 10) // condition
Step 4 : print n
n = n + 1 // updation
Step 5 : stop
Flowchart
start Problem Solving Tools
n=1
n <= 10 F
stop
print n
n=n+1
Print first n natural numbers
Algorithm
Step 1 : start
Step 2 : input num
Step 3 : n = 1
Step 4 : repeat step 5 while (n ≤ num)
Step 5 : print n
n=n+1
Step 6 : stop
Flowchart
start Problem Solving Tools
input num
n= 1
n <= num F
stop
print n
n=n+1
Print sum of first n natural numbers
Algorithm
Step 1 : start
Step 2 : input n
Step 3 : sum = 0, c = 1
Step 4 : repeat step 5 while (c ≤ n)
Step 5 :
sum = sum + c
c=c+1
Step 6 : print sum
Step 7 : stop
Flowchart Problem Solving Tools
start
input n
sum = 0, c = 1
c <= n F print sum stop
sum = sum + c
c=c+1
Print factorial of a given number
Algorithm
Step 1 : start
Step 2 : input n
Step 3 : fact = 1, c = 1
Step 4 : repeat step 5 while (c ≤ n)
Step 5 :
fact = fact * c
c=c+1
Step 6 : print fact
Step 7 : stop
Flowchart
start
Problem Solving Tools
input n
fact = 1, c = 1
c <= n F print fact stop
fact = fact * c
c=c+1
Factorial of a number the right way ↓
Print factorial of a given number
Algorithm
Step 1 : start
Step 2 : input n
Step 3 : fact = 1, c = n
Step 4 : repeat step 5 while (c ≥ 1)
Step 5 :
fact = fact * c
c=c-1
Step 6 : print fact
Step 7 : stop
Flowchart
start
Problem Solving Tools
input n
fact = 1, c = n
F print fact stop
c >= 1
fact = fact * c
c=c-1
For loop
for loop
Print factorial of a given number using 'for loop'
start Problem Solving Tools
input n
'c' is incrementing
fact = 1
one by one
for( c = n to 1 ), step 1
fact = fact * c
print fact
stop
Print factorial of a given number using 'for loop' (decrement)
start Problem Solving Tools
input n
'c' is
fact = 1
decrementing
for( c = n to 1 ), step -1
fact = fact * c
print fact
stop