Algorithm Exercises

Algorithm Exercises

Algorithm Exercises

1) Develop an algorithm program to add two number.

  1.    start
  2.    Input the two number a and b
  3.    Calculation the sum as a+b
  4.    Store the result in sum
  5.    Display the result
  6.    Stop

For an Algorithm definition click here…

2) Write an algorithm to calculate the factorial of a given number.

  1.     Start
  2.     Read the number n
  3.     [Initialize] i=1,fact=1
  4.     Repeat steps 4 through 6 until i=n
  5.     fact=fact*i
  6.     i=i+1
  7.     Print fact
  8.     Stop

3) Write an algorithm to compute and print the average of a set of data values.

  1.      Start
  2.      Set the sum of the data values and the count to zero
  3.      As long as the data values exist, add the next data value to the sum and add 1 to the count
  4.      To compute the average, divide the sum by the count
  5.      display the average
  6.      Stop

4) Write an algorithm to check palindrome number.

  1.      Start
  2.      Read: N
  3.      Initialize R = 0
  4.     Repeat While N ≠ 0
    Compute: R = R*10 + R%10
    Compute: N = N/10
    [ End of Loop ]
  5. Check: If N == R Then
    Print: N is a Palindrome Number.
    Else
    Print: N is not a Palindrome Number.
    [ End of If Else Structure ]
  6.     Stop

5) Write an algorithm to check armstrong number.

  1.      Start
  2.      Take Input N as Integer
  3.      [ initializing SUM ] Set: SUM = 0
  4.      Set: Number = N
  5. Repeat While Number ≠ 0
    SUM = (Number%10)**3 + SUM
    Number = Number/10
    [ End of While Loop ]
  6. [ Checking ? ] If SUM == N then
    Print: N is an Armstrong Number.
    Else
    Print: N is not an Armstrong Number.
    [End of If-Else Structure]
  7.      Stop