Page 421 - computer science (868) class 11
P. 421

3.  Write any two characteristics of a good algorithm.
                   Ans.  a.  Accuracy: A good algorithm must provide the correct solution to a given problem in an unambiguous manner.
                        b.  Finiteness: A good algorithm must produce the correct output after finite number of steps and terminate after that.
                    4.  Write two advantages of an algorithm.
                   Ans.  a.  Every step in an algorithm follows a logical sequence, so it helps in program development and debugging.
                        b.  The algorithm provides a proper understanding of the input requirement, output and flow of the process.
                    5.  Write two disadvantages of an algorithm.
                   Ans.  a.  Developing an algorithm is a time consuming process.
                        b.  Complex algorithms are very difficult to design and understand.
                    6.  Write an algorithm to calculate and print the sum of odd numbers up to that number. Sum = 1 + 3 + 5 + 7 + ……… + n
                   Ans.  Step 1: Start
                       Step 2: Accept number n.
                       Step 3: Initialise the sum to 0.
                       Step 4: Initialise i to 1.
                       Step 5: Repeat Step 6 to Step 7 while i <= n.
                       Step 6: Add i to sum.
                        Step 7: Increase i by 2.
                       Step 8: Display sum.
                       Step 9: Stop
                    7.  Write an algorithm to print the lowest common multiple of two numbers.
                   Ans.  Step 1: Start
                       Step 2: Initialise variables lcm and i to 1.
                        Step 3: Input two numbers num1 and num2.
                       Step 4: Repeat Step 5 to Step 6 while i <= num1 × num2.
                       Step 5: If lcm % num1 = 0 && lcm % num2 = 0 then assign lcm = i, and go to Step 7.
                       Step 6: Increment variable lcm by 1 and i by 1.
                       Step 7: Print lcm.
                       Step 8: Stop
                    8.  Write an algorithm to check if a number is perfect number or not. A perfect number is a number that is equal to the sum of its
                       factors excluding itself. For example,
                        6 is a perfect number. Factors of 6 are 1, 2, 3.
                        Sum of factors excluding 6 is 1 + 2 + 3 = 6
                        28 is a perfect number. Factors of 28 are 1, 2, 4, 7, 14, 21.
                        Sum of the factors excluding 28 are 1 + 2 + 4 + 7 + 14 = 28
                   Ans.  Step 1: Start
                       Step 2: Initialise variable sum to 0.
                       Step 3: Read number num.
                       Step 4: Repeat Step 5 to Step 7 while i < num.
                       Step 5: If num is divisible by i then go to Step 6, else go to Step 7.
                        Step 6: Add i to sum.
                       Step 7: Increment i by 1.
                       Step 8: If sum = num then go to Step 9, else go to Step 10.
                       Step 9: Display num “is a perfect number”, go to Step 11.
                       Step 10: Display num “is not a perfect number”.
                       Step 11: Stop





                                                                                                                       419
                                                                            Implementation of Algorithms to Solve Problems  419
   416   417   418   419   420   421   422   423   424   425   426