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
- 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.
- 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.
- Definiteness ⇒ Each step should be unique
- No step should be repeated
- Each step must be clear and precisely defined.
- 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.
- Correctness ⇒ Output should be correct.
3 Types of Algorithm
- Sequential
- Conditional
- 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
- 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.
Sum of two numbers
Area of rectangle
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
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
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
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
- print number is multi digit
- Step 4 : stop
Flowchart
Check whether a number is even or odd.
Algorithm
- Step 1 : start
- Step 2 : input n
- Step 3 : if n % 2 == 0
- Step 4 : stop
Flowchart
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
- print n is positive number
- Step 4 : stop
Flowchart
(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
- Step 3 : percentage = (m1 + m2 + m3 + m4)/4
- Step 4 : if (percentage >= 70)
- print 1st division with honours
- else if (percentage >= 60)
- else if (percentage >= 50)
- else if (percentage >= 40)
- Step 5 : stop
Flowchart
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
- 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
- Step 5 : stop
Flowchart
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
- Step 6 : stop
Flowchart
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 :
- Step 6 : print sum
- Step 7 : stop
Flowchart
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
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
For loop
Print factorial of a given number using 'for loop'
Print factorial of a given number using 'for loop' (decrement)