Page 109 - Computer science 868 Class 12
P. 109

Problem 5: Write an algorithm to print the highest common factor of two numbers.
                 Step 1:  Start.

                 Step 2:  Initialise variable gcd and i to 1.

                 Step 3:  Input two numbers in num1 and num2.
                 Step 4:  Repeat Step 5 to Step 6 until i <= num1 and i <= num2.

                 Step 5:  If num1 % i == 0 && num2 % i == 0 then assign gcd = i.
                 Step 6:  Increment variable i by 1.

                 Step 7:  Print gcd.
                 Step 8:  Stop.


                 Problem 6: Write an algorithm to input any number and print its prime factors. Prime factor of 16 is 2×2×2×2.
                 Step 1:  Start.

                 Step 2:  Accept number n.
                 Step 3:  Assign copy c=n and factor f=2.

                 Step 4:  Repeat step 5 to step 8 while c>1.
                 Step 5:  if c%f=0 then go to step 6 else go to step 8.

                 Step 6:  Print f.

                 Step 7:  Assign c=c/f, go to Step 4.
                 Step 8:  Increase f by 1, go to Step 4.

                 Step 9:  Stop.

                 Problem 7: Write an algorithm to print the first 10 terms of a Fibonacci series. A Fibonacci series is a series in which
                 any term is the sum of its previous two terms. The first two terms are 0 and 1. The first ten terms of a Fibonacci
                 series are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34.
                 Step 1:  Start.

                 Step 2:  Initialise variables term1 to 0, term2 to 1, term3 to 0 and i to 1.
                 Step 3:  Read the number of terms in num.

                 Step 4:   Display term1 and term2.
                 Step 5:  Repeat Step 5 to Step 9 until i <= num -2.

                 Step 6:  Calculate term3 = term1 + term2.

                 Step 7:  Display term3.
                 Step 8:  Initialise term1 = term2.

                 Step 9:  Initialise term2 = term3
                 Step 10:  Increment i by 1.

                 Step 11:  Stop.





                                                                                                                       107
                                                                            Implementation of Algorithms to Solve Problems  107
   104   105   106   107   108   109   110   111   112   113   114