Page 449 - Cs_withBlue_J_C11_Flipbook
P. 449

9.  Write an algorithm to check if a number is Krishnamurthy number or not. Krishnamurthy number is a number that is equal to
                       the sum of the factorial of its digits. Let us demonstrate it with an example.
                        Say, Input = 145 is a Krishnamurthy number
                        Now, 1! +  4! + 5!
                        = 1 + (1 × 2 × 3 × 4) + (1 × 2 × 3 × 4 × 5)
                        = 1 + 24 + 120
                        = 145
                   Ans.  Step 1: Start
                       Step 2: Read number num.
                       Step 3: Initialise variables copy to num and sum to 0.
                       Step 4: Repeat Step 5 to Step 11 while copy > 0.
                       Step 5: Assign digit = copy % 10.
                       Step 6: Initialise i and factorial to 1.
                       Step 7: Repeat Step 8 to Step 9 while i <= digit.
                       Step 8: Multiply i with factorial and store the product in factorial.
                       Step 9: Increment i by 1.
                       Step 10: Add factorial to sum.
                       Step 11: Divide copy by 10 and store quotient in copy.
                       Step 12: If sum = num then go to Step 13, else go to Step 14.
                       Step 13: Display num “is a Krishnamurthy number”, and go to Step 15.
                       Step 14: Display num “is not a Krishnamurthy number”.
                       Step 15: Stop
                    10.  Write an algorithm to delete any number from position ‘pos’ of a single dimensional array a[]. Assume that the array is already
                       created.
                   Ans.  Step 1:  Start
                       Step 2:  Initialise i to pos.
                       Step 3:  Repeat Step 4 to Step 5 while i < arraylength - 1.
                        Step 4:  Assign a[i] = a[i + 1].
                       Step 5:  Increment i by 1.
                       Step 6:  Decrement the size of the array by 1.
                       Step 7:  Display array.
                       Step 8:  Stop
                    11.  Write an algorithm to create a transpose matrix b[][] from the original square matrix a[][] of size s which is already created.
                       Transpose of a matrix is obtained by interchanging row and column.
                   Ans.  Step 1:  Start
                        Step 2:  Initialise i to 0.
                        Step 3:  Repeat Step 4 to Step 8 until i < s.
                        Step 4:  Initialise j to 0.
                        Step 5:  Repeat Step 6 to Step 7 while j < s.
                        Step 6:  Assign b[j][i] = a[i][j] .
                        Step 7:  Increment j by 1.
                        Step 8:  Increment i by 1.
                        Step 9:  Display matrix b[][].
                        Step 10: Stop
                    12.  Accept a word and check it is a palindrome or not. A palindrome is a word that reads the same from both ends. Example
                       MADAM, NOON, MALAYALAM, etc.
                   Ans.  Step 1:  Start
                       Step 2:  Accept the word in variable word.
                       Step 3:  Initialise i to 0 and reverseword to null.



                                                                                                                       447
                                                                            Implementation of Algorithms to Solve Problems  447
   444   445   446   447   448   449   450   451   452   453   454