Monday, September 27, 2010

Algorithms

USING ALGORITHMS TO SOLVE PROBLEMS:

What is an Algorithm?
An algorithm is a step-by-step procedure to solve a given problem.
Steps in algorithm writing:
Ø  Identify all the inputs and outputs required for solving a problem.
Ø  Identify additional data and constants needed.
Ø  Computation steps
Ø  Print / Display all the required results.

DIFFERENT PATTERNS IN ALGORITHMS:
There are mainly three different patterns of algorithms. They are as listed below-
  1. Sequential – The steps represent sequence of occurrence of processing.
  2. Conditional – Based on one or more conditions the instructions are divided
  3. Iterational – Same set of instructions are repeated more than once until an end condition. These are also called Repetitive patterns.



EXAMPLES:
A. On Sequential Patterns
1.    Write an algorithm to find the sum of any two numbers.
Sol-                 Step 1: Start
                        Step 2: Read two numbers n1 and n2
                        Step 3: Sum = n1 + n2
                        Step 4: Print ‘Sum’
                        Step 5: Stop

2.    Write an algorithm to find the average of three numbers.
Sol-                 Step 1: Start
                        Step 2: Read three numbers n1, n2 and n3
                        Step 3: Avg = (n1 + n2 + n3) /3
                        Step 4: Print ‘Avg’
                        Step 5: Stop

3.    Write an algorithm to find the volume of a cylinder.
Sol-                 Step 1: Start
                        Step 2: Read radius (r) and height (h)
                        Step 3: Initialize constant,  P = 3.142
                        Step 4: Volume = Pr2h
                        Step 5: Print ‘Volume’
                        Step 6: Stop

4.    Write an algorithm to find the area and perimeter of a square.
Sol-                 Step 1: Start
                        Step 2: Read side of a square (a)
                        Step 3: Area = a2      and     Perimeter = 4a
                        Step 4: Print ‘Area’ and ‘Perimeter’
                        Step 5: Stop

5.    Write an algorithm to convert celcius into farenheit.
Sol-                 Step 1: Start
                        Step 2: Read temperature in celsius (c)
                        Step 3: f = 9c / 5 + 32
                        Step 4: Print temperature in farenheit (f)
                        Step 5: Stop

B. On Conditional Patterns
1.    Write an algorithm to find biggest of two numbers.
Sol-                 Step 1: Start
                        Step 2: Read two numbers (n1 and n2)
                        Step 3: if n1 > n2, go to Step 5
                        Step 4: Print “n2 is biggest” and go to Step 6
                        Step 5: Print “n1 is biggest”
                        Step 6: Stop

2.    Write an algorithm to check whether a given number is +ve, -ve or a zero.
Sol-                 Step 1: Start
                        Step 2: Read a number (n)
                        Step 3: if n > 0, go to Step 6
                        Step 4: if n < 0, go to Step 7
                        Step 5: Print “n is Zero” and go to Step 8
                        Step 6: Print “n is a +ve number” and go to Step 8
                        Step 7: Print “n is a –ve number”
                        Step 8: Stop

3.    Write an algorithm to check whether a given number is even or odd.
Sol-                 Step 1: Start
                        Step 2: Read a number (n)
                        Step 3: if remainder of n/2 is = 0, go to Step 5 
                                     or if n % 2 = 0, go to Step 5
                        Step 4: Print “n is odd” and go to Step 6
                        Step 5: Print “n is even”
                        Step 6: Stop

4.    Write an algorithm to find a person’s eligibility for voting, based on his/her age.
Sol-                 Step 1: Start
                        Step 2: Read the age of a person (age)
                        Step 3: if age > 18, go to Step 5
                        Step 4: Print “Person is not eligible for voting” and 
                                     go to Step 6
                        Step 5: Print “Person is eligible for voting”
                        Step 6: Stop

C. On Iterational Patterns
1.    Write an algorithm to print a number (5) 10 times.
Sol-                 Step 1: Start
                        Step 2: Initialize Counter to 1 (Count =1)
                        Step 3: Print ‘5’
                        Step 4: Count = Count + 1
                        Step 5: if Count < = 10 go to Step 3
                        Step 6: Stop

2.    Write an algorithm to print all the odd numbers between 1 and 10.
Sol-                 Step 1: Start
                        Step 2: Initialize Counter to 1 (Count =1)
                        Step 3: Print ‘Count’
                        Step 4: Count = Count + 2
                        Step 5: if Count < = 10 go to Step 3
                        Step 6: Stop

3.    Write an algorithm to print all the even numbers between 1 and 10.
Sol-                 Step 1: Start
                        Step 2: Initialize Counter to 2 (Count =2)
                        Step 3: Print ‘Count’
                        Step 4: Count = Count + 2
                        Step 5: if Count < = 10 go to Step 3
                        Step 6: Stop

1 comment:

  1. Madam,Why don't you put some Aptitude questions to solve in C?

    ReplyDelete