MCPcopy Create free account
hub / github.com/geekcomputers/Python / addition

Function addition

Sum of digits of a number.py:26–48  ·  view source on GitHub ↗

Returns the sum of the digits of a number. Negative numbers are handled using the absolute value. Examples: >>> addition(123) 6 >>> addition(-784) 19

(num)

Source from the content-addressed store, hash-verified

24
25
26def addition(num):
27 """
28 Returns the sum of the digits of a number.
29 Negative numbers are handled using the absolute value.
30
31 Examples:
32 >>> addition(123)
33 6
34 >>> addition(-784)
35 19
36 """
37 Sum = 0
38 if type(num) is type(
39 None
40 ): # Checks if number type is none or not. If type is none program exits.
41 print("Try again!")
42 sys.exit()
43 num = abs(num) # Handle negative numbers
44 while num > 0: # Addition- adding the digits in the number.
45 digit = int(num % 10)
46 Sum += digit
47 num //= 10
48 return Sum # Returns sum to where the function is called.
49
50
51if (

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected