Computer Science, asked by vidushijoshi1234, 9 months ago

Function in python to find sum of all digits

Answers

Answered by nagpaldj
2

Answer:

The sum of the digits of a number means adding up each of the digits and returning the sum. For instance, the sum of the digits of 123 is 6.

ITERATE OVER EACH DIGIT

Use str() to convert a number to a string. Create a for-loop to iterate over each digit in the string. Use int() to convert the string digit to an integer. Add this digit to the sum of the digits in each iteration.

number = 123

sum_of_digits = 0

for digit in str(number):

turn to string to iterate through

sum_of_digits += int(digit)

print(sum_of_digits)

OUTPUT

6

Explanation:

plz mark it as brainlist

Answered by villainempire700
0

Answer:

Simply add these numbers or if you take input from the user then type int(number for you want the sum) and orint it

Similar questions