× Back Algorithm Flowchart Looping
Next Topic →

Problem Solving Tools

Algorithm

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
  • 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

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 
            else 
            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
            print n is even 
            else 
            print n is odd
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
            else if (n > 0)
            print n is positive number
            else 
            print n is zero 
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)
            print 1st division
            else if (percentage >= 50)
            print 2nd division
        else if (percentage >=40)
            print 3rd division 
        else 
            print failed
step 5 : stop
                            
                        

Flowchart

Looping

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

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

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

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'

Flowchart

Print factorial of a given number using 'for loop' (decrement)

Flowchart