Page 215 - Computer Science Class 11 Without Functions
P. 215
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 9.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
9.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. For this purpose, we begin with a clean slate, i.e., we initialise reverseNum as 0. Next, we
extract the unit's digit (4) and add it to reverseNum. So, reverseNum becomes 4. Now that we do not require the unit's
digit any more, we discard it by setting num = num // 10. Now num is equal to 123. Again, we multiply reverseNum
by 10 and add to it the unit's digit (3) from the current value of num (123). Thus, reverseNum becomes 43. Again,
discarding the units's digit of num, we get num equal to 12. We multiply reverseNum by 10 and add to it the unit's digit
(2) from the current value of num (12). Thus, reverseNum becomes 432. Now, discarding the units's digit (2) of num (12),
we get the num equal to 1. Again, we multiply reverseNum by 10 and add to it the unit's digit (1) from the current value
of num (1). Thus, reverseNum becomes 4321. Now, discarding the units's digit of num, we get num equal to 0, which
indicates that there are no more digits to be processed and we have been able to build reverseNum as desired.
Looping in Python 213

