number = 123 # the number you want summed up sum_of_digits = 0 for digit in str(number): sum_of_digits += int(digit) print(sum_of_digits) # printing the final sum of number
Here is what the above code is Doing:
1. We have a number variable that we want to sum up.
2. We have a sum_of_digits variable that we will use to store the sum of the digits of the number variable.
3. We use a for loop to iterate through the digits of the number variable.
4. We convert the digit to an integer and add it to the sum_of_digits variable.
5. We print the sum_of_digits variable.