Python - sum of digits till the result becomes single digit Ex: 479 - 20 - 2
AnswerBot
1y
Python program to find the sum of digits till the result becomes a single digit.
Convert the number to a string and iterate through each digit.
Add the digits and store the result.
Repeat the process unt...read more
Abhilash Gajbhiye
1y
x= int(input("please enter the no"))
while len(str(x))>1:
l=[]
for i in str(x):
l.append(int(i))
x=sum(l)
print(x)
Vineetha Chintala
1y
static code
num = 123
digits = [int(digit) for digit in str(num)]
print(digits)
print(sum(digits))
dynamic code:
def digits_sum(num):
while num >= 10:
digits = [int(digit) for digit in str(num)]
num = sum(...read more
Debasish Patel
1y
def digital_root(num):
if num == 0:
return 0
else:
return 1 + ((num - 1) % 9)
Debasish Patel
1y
result = digital_root(479)
print(result) # Output: 6
Debasish Patel
1y
def digital_root(num):
while num >= 10:
num = sum(int(digit) for digit in str(num))
return num
Debasish Patel
1y
def digital_root(num):
while num >= 10:
num = sum(int(digit) for digit in str(num))
return num
Deepak Kumawat
2y
def sum(value):
return (value - 1) % 9 + 1 if value > 0 else 0
Add answer anonymously...
Popular interview questions of Data Engineer
Top HR questions asked in Futurense Technologies Data Engineer
>
Futurense Technologies Data Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app