Algorithm: Examples

1. Algorithm to Display Addition of 2 Numbers:

 In this example in order to calculate sum we required 2 input variables named a , b and one output variable named sum.
  1. Start
  2. Declare a , b and sum
  3. Read a and b
  4. sum=a+b
  5. display sum
  6. Stop
2. Algorithm to Check given number is Even or Odd :

For this example we required only one input number to check it is even or odd. We need to find the remainder when we are dividing that number by 2. Based on remainder we will decide number is even or odd. To calculate remainder we will use % operator.
  1. Start
  2. Declare no
  3. Read no
  4. If(no%2==0)
    1. Display “no is EVEN”
Else
1. Display “no is ODD”
5.      Stop

3.  Algorithm to Display Number between 1 to 100 :

This example requires looping concept. We will use one variable named no to keep the track that how many numbers are displayed. We will initialize that no to 1 and keep incrementing its value by 1 till it becomes 100. Once its values become 100 we will exit from the loop and program will terminate.
  1. Start
  2. Declare no
  3. Set no=1
  4. Repeat steps until n becomes 101
    1. Display  no
    2. no=no+1
5.      Stop
OR
  1. Start
  2. Declare no
  3. Set no=1
  4. If(n<101)
    1. Display  no
    2. no=no+1
    3. goto step no 4
5.      Stop

No comments:

Post a Comment