7
Type a program that receives an integer number in the input, calculate and print the sum of the digits of this number in the output. Example:
>>> Digite um número inteiro: 123
6
Tip: To separate digits, remember: the operator //
makes an entire division by throwing away the rest, ie that which is less than the divisor; The operator %
returns only the rest of the entire division by throwing away the result, that is, all that is greater than or equal to the divisor.
I tried several ways, but the result keeps giving zero. I only have this attempt noted because I’ve been doing over:
n = int(input("Digite um número inteiro: "))
soma = 0
while (n > 0):
resto = n % 10
n = (n - resto)/10
soma = soma + resto
print("A soma dos números é: ", n)
1-only bar is floating point division, you should use double bar (integer division); 2- not integer division, you do not need to subtract the rest
– Jefferson Quesado
n = n//10
using the entire split operator without taking the rest– Jefferson Quesado