Page 441 - Cs_withBlue_J_C11_Flipbook
P. 441

Step 9:  Increment i by 1.
                 Step 10:  Stop.

                 Problem 8: Write an algorithm to check if a number is a palindrome or not. A palindrome number is a number that
                 is equal to its reverse. For example,
                 number = 252, its reverse = 252. So, it is a palindrome number.
                 number = 253, its reverse = 352. So, it is not a palindrome number.

                 number = 4554, its reverse = 4554. So, it is a palindrome number.
                 number = 4556, its reverse = 6554. So, it is not a palindrome number.
                 Step 1:  Start.

                 Step 2:  Read number num.
                 Step 3:  Initialise variables copy to num, and reverse to 0.
                 Step 4:  Repeat Step 5 to Step 7 until copy > 0.

                 Step 5:  Divide copy by 10 and store remainder to dig.
                 Step 6:  Calculate reverse *10 + dig and accumulate the result in reverse.
                 Step 7:  Divide copy by 10 and store its quotient in copy.

                 Step 8:  If num = reverse then go to Step 9, else go to Step 10.
                 Step 9:  Display num “is a palindrome number”, go to Step 11.
                 Step 10: Display num “is not a palindrome number”.

                 Step 11: Stop.
                 Problem 9: Write an algorithm to check if a number is a Happy number or not. Happy number is a number in which
                 the ultimate sum of the square of the digits is equal to 1. Let us demonstrate it with an example.
                 Say Input = 28

                  2
                      2
                 2  + 8  = 68
                 Since 68 is not a one-digit number, calculate the sum of the square of digits of 68
                  2
                      2
                 6  + 8  = 100.
                 Since 100 is not a one-digit number, further digit extraction is possible.
                      2
                  2
                          2
                 1  + 0  + 0  =1
                 As we have reached a one-digit number, no more extraction of digit is required. Thus, since the ultimate sum is 1,
                 so 28 is a Happy number.
                 Step 1:  Start.

                 Step 2:  Read number num.
                 Step 3:  Initialise variable copy to num.

                 Step 4:  Repeat Step 5 to Step 10 while copy > 9.
                 Step 5:  Initialise the sum to 0.

                 Step 6:  Repeat Step 7 to Step 9 while copy > 0.
                 Step 7:  Divide copy by 10 and store remainder to dig.
                 Step 8:  Calculate dig*dig and accumulate the result in sum.



                                                                                                                       439
                                                                            Implementation of Algorithms to Solve Problems  439
   436   437   438   439   440   441   442   443   444   445   446