Page 150 - Information_Practice_Fliipbook_Class11
P. 150

6.4.1 Summing the Digits of a Number

        Now we are ready to study an application of the while statement. Suppose we wish to find the sum of digits of a
        number. For this purpose, we write a program that accepts as input the number num, whose sum of digits we wish
        to find (see Program 6.5). As we are only interested in finding the sum of the digits of num, we ignore the sign of
        the number by taking its absolute value (line 14). Next, we initialise the sum of the digits (digitSum) as zero.
        If the number whose sum of digits we want to find is zero, the condition num > 0 fails at the beginning of the
        execution of the while statement. In this case, the execution of the while statement finishes without executing
        the body of the while statement even once. However, if the condition num > 0 yields True at the beginning of
        the while statement,  we find the digits one by one beginning at the unit's place. To find the digit at the unit's place,
        we compute the remainder (num % 10) on division by 10  and add it to the digit sum accumulated so far (line 17).
        As the role of the digit at the unit's place is now over, we drop it by dividing num (integer division) by 10 (line 18).
        Note that the number you get when you divide the original number by 10 still has all the relevant information. It has
        all the digits except for the one at the unit's place, which we have already taken into account. Also, the digit that was
        at the 10's position in the original number is now at the unit position in the number that is obtained by dividing the
        original number by 10. So, we now need to find the sum of the digits of the updated value of num and add it to the
        sum accumulated in digitSum. This process of finding the digit at the unit's place, adding it to  the sum accumulated
        in digitSum, and updating the value of num continues as long as num>0 (line 16). When num becomes 0, the
        control leaves the while statement and the program outputs the computed value of the sum of digits (digitSum).

         Program 6.5 Write a program to compute sum of digits of a number.
          01 '''
          02 Objective: To compute sum of digits of a number
          03 Input Parameter: num
          04 Output: sum of digits of num
          05
          06 Approach:
          07     Ignore the sign of the number. Initialise digit Sum to
          08     zero. Extract digits one by one beginning unit's
          09     place and keep on adding it to the digitSum.
          10 '''
          11
          12 num = int(input('Enter a number: '))
          13
          14 num = abs(num)
          15 digitSum = 0
          16 while num > 0:
          17     digitSum += (num % 10)
          18     num = num // 10
          19
          20 print('Sum of digits is: ', digitSum)
        Sample Output:

         >>> Enter a number: 4321
              Sum of digits is: 10
        6.4.2 A Number with Digits in Reverse Order

        Next, let's write a program that takes a non-negative integer as an input and outputs a number whose digits are in the
        opposite order of the argument's digits. For example, reverse of 1234 will output 4321. Before we jump into the
        coding task, let us understand the process of obtaining a number (say, reverseNum) whose digits are in the opposite
        order of the argument's digits. Note that 1234 = ((1x10 + 2)x10 + 3)x10+4. So, to build reverseNum, we must compute
        ((4x10 + 3)x10 + 2)x10+1.



          136  Touchpad Informatics Practices-XI
   145   146   147   148   149   150   151   152   153   154   155