Python Challenges: Add the digits of a positive integer repeatedly until the result has a single digit
Write a Python program to add the digits of a positive integer repeatedly until the result has a single digit.
Explanation :
Sample Solution:
Python Code:
def add_digits(num):
return (num - 1) % 9 + 1 if num > 0 else 0
print(add_digits(48))
print(add_digits(59))
Sample Output:
3 5
Flowchart:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to find two elements once in a list where every element appears exactly twice in the list.
Next: Write a Python program to find whether it contains an additive sequence or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics