-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSum_Integers[1].py
More file actions
27 lines (21 loc) · 821 Bytes
/
Sum_Integers[1].py
File metadata and controls
27 lines (21 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding: utf-8 -*-
'''
Lawrence Woods
'''
'''
(Sum the digits in an integer) Write a program that reads an integer
between 0 and 1000 and adds all the digits in the integer. For example,
if an integer is 932, the sum of all its digits is 14.
(Hint: Use the % operator to extract digits, and use the // operator
to remove the extracted digit. For instance, 932 % 10 = 2 and 932 // 10 = 93.)
Here is a sample run:
Enter a number between 0 and 1000: 999
The sum of the digits is 27
'''
n = eval (input ("Enter a number between 0 and 1000:"))
(quotient, remainder,) = divmod(n, 10)
sumOfDigits = remainder
while quotient: # same as while quotient != 0:
(quotient, remainder,) = divmod(quotient, 10)
sumOfDigits += remainder
print ("The sum of the digits is" ,sumOfDigits )