Page 308 - Computer Science V2.0 Class 11
P. 308

On execution of Line 14, we initialise the sum of the digits (digitSum) as zero.
                 On execution of Line 15:
                ○  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.
                ○ If the condition num > 0 yields True, it will execute the body of the while statement from the beginning.

                 On execution of line 16, we compute the remainder (num % 10) by dividing by 10 and adding it to the digit sum
                accumulated.

                 On execution of line 17, the digit at the unit's place is now over, we drop it by dividing num (integer division) by 10.
                 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.
                 Line 15, 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.
                 When num becomes 0, the control leaves the while statement, and the function sumOfDigits() returns the
                computed value of the sum of digits (digitSum) at line 23.
                 On execution of line 24, the sum of digits is displayed, and the program comes to an end.

               Program 11.5 Write a function sumOfDigits(num) that computes value of the sum of digits.

                01 def sumOfDigits(num):
                02     '''
                03     Objective: To compute sum of digits of a number
                04     Input Parameter: num - numeric
                05     Return Value: numeric
                06     '''
                07     '''
                08     Approach:
                09         Ignore the sign of the number. Initialize digit Sum to
                10         zero. Extract digits one by one beginning unit's
                11         place and keep on adding it to digit sum.
                12     '''
                13     num = abs(num)
                14     digitSum = 0
                15     while num > 0:
                16         digitSum += (num % 10)
                17         num = num // 10
                18     return digitSum
                19 # main program segment
                20 # Objective: To compute sum of digits of a user-provided input
                21 num = int(input('Enter a number: '))
                22 digitSum = sumOfDigits(num)
                23 print('Sum of digits of', num, 'is', digitSum)
              Sample Output:

               >>> Enter a number: 4321
                    Sum of digits of 4321 is 10




               294   Touchpad Computer Science (Ver. 2.0)-XI
   303   304   305   306   307   308   309   310   311   312   313