How to make loop repeat until the sum is a single digit?

后端 未结 8 1883
无人及你
无人及你 2020-12-21 02:26

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 numb

8条回答
  •  北海茫月
    2020-12-21 02:35

    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)
    

提交回复
热议问题