4
votes

Prompt: Write a program that adds all the digits in an integer. If the resulting sum is more than one digit, keep repeating until the sum is one digit. For example, the number 2345 has the sum 2+3+4+5 = 14 which is not a single digit so repeat with 1+4 = 5 which is a single digit.

This is the code I have so far. It works out for the first part, but I can't figure out how to make it repeat until the sum is a single digit. I'm pretty sure I'm supposed to nest the code I already have with another while statement

n = int(input("Input an integer:"))
sum_int=0  
while float(n)/10 >= .1:   
    r= n%10
    sum_int += r
    n= n//10   
    if float(n)/10 > .1: print(r,  end= " + ") 
    else: print(r,"=",sum_int)

this is a sample output of the code

Input an integer: 98765678912398

8 + 9 + 3 + 2 + 1 + 9 + 8 + 7 + 6 + 5 + 6 + 7 + 8 + 9 = 88

8 + 8 = 16

1 + 6 = 7

8
Seems like an excellent opportunity to use recursion. - Luke Joshua Park
use an if statement and len() - karatechop
Why are you having your program do all that arithmetic? Turn a number into a string, then turn each character into a digit. - TigerhawkT3
For the termination condition: what can be said about the numeric value of a 'single decimal digit'? How can this be used in context? This can, and probably should, be solved without the use of floats or strings. - user2864740
no more one digit like this? while n>9 - Ali

8 Answers

6
votes

This should work, no division involved.

n = int(input("Input an integer:"))
while n > 9:
    n = sum(map(int, str(n)))
print(n)

It basically converts the integer to a string, then sums over the digits using a list comprehension and continues until the number is no greater than 9.

3
votes

you can try this solution, if n=98 then your output will be 8

def repitative_sum(n):
    j=2
    while j!=1:
        n=str(n)
        j=len(n)
        n=list(map(int,n))
        n=sum(n)
    print(n)
2
votes

You don't need to convert your integer to a float here; just use the divmod() function in a loop:

def sum_digits(n):
    newnum = 0
    while n:
        n, digit = divmod(n, 10)
        newnum += digit
    return newnum

By making it a function you can more easily use it to repeatedly apply this to a number until it is smaller than 10:

n = int(input("Input an integer:"))
while n > 9:
    n = sum_digits(n)

print(n)
2
votes

You could utilize recursion.

Try this:

def sum_of_digits(n):
    s = 0

    while n:
        s += n % 10
        n //= 10

    if s > 9:
        return sum_of_digits(s)

    return s

n = int(input("Enter an integer: "))
print(sum_of_digits(n))
1
votes

I'm not sure if it's anti-practice in Python because I know nothing about the language, but here is my solution.

n = int(input("Input an integer:"))

def sum_int(num):
    numArr = map(int,str(num))
    number = sum(numArr)
    if number < 10:
        print(number)
    else:
        sum_int(number)

sum_int(n)

Again I am unsure about the recursion within a function in Python, but hey, it works :)

1
votes

If you like recursion, and you must:

>>> def sum_digits_rec(integ):
        if integ <= 9:
            return integ
        res = sum(divmod(integ, 10)) 
        return sum_digits(res)

>>> print(sum_digits_rec(98765678912398))
7
0
votes
def digit_sum(num):
    if num < 10:
        return num
    last_digit = num % 10
    num = num / 10
    return digit_sum(last_digit + digit_sum(num))

input_num = int(input("Enter an integer: "))
print("result : ",digit_sum(input_num))

This may help you..!

0
votes
def add_digits(num):
        return (num - 1) % 9 + 1 if num > 0 else 0

A simple, elegant solution.