Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python adding digits

def sum_digits(num: int) -> int:
	
	#base case when your "positive" integer get to 0
    if num == 0: 
        return 0
    #base case when your "negative" integer get is between -10 and 0
    if num > -10 and num < 0:
        return num
	
	# recursion for positive integer
    elif num > 0:
        return (num % 10) + sum_digits(num//10)
	
	# recursion for negative integer
    elif num < 0:
        return -(abs(num) % 10) + sum_digits(-(abs(num)//10))
      
sum_digits(123) # returns 6
sum_digits(-123) # returns -6
Source by kite.com #
 
PREVIOUS NEXT
Tagged: #Python #adding #digits
ADD COMMENT
Topic
Name
2+3 =