I want to solve this but I have no idea how to
I´d appreciate your help
given n take the sum of the digits of n if that value has more than one digit continue until there´s only one
Expected output:
16 -> 1 + 6 = 7
942 -> 9 + 4 + 2 = 15 -> 1 + 5 = 6
I tried this but I don´t know how to repeat it untill there is only one digit
Def sum_digit(n):
list_of_digits = list(map(int,str(n)))
su = []
for x in list_of_digits:
x = sum(list_of_digits)
su = x
print(su)
sum_digit(6784)
You can use a while
loop to repeat until the number reduce to a single digit.
def sum_digit(n):
while n > 9:
n = sum(int(i) for i in str(n))
return n
sum_digit(16)
#7
sum_digit(942)
#6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With