Receive an integer number in the input, calculate and print the sum of the odd digits of this number in the output

Asked

Viewed 390 times

-1

I saw a similar doubt and he used this code, but in my case I need him to add up only the odd numbers he gets.

For example, when typing 123, instead of adding up 1+2+3 it should add up 1+3 only. How would it look?

x = int(input("Numero: "))

soma = 0

while (x != 0):
    resto = x % 10
    x = (x - resto)//10
    soma = soma + resto
print(soma)

2 answers

1

You almost right, just checking if the digit is odd:

x = abs(int(input("Numero: ")))
soma = 0
while x != 0:
    resto = x % 10
    if resto % 2 != 0: # só adiciona à soma se for ímpar
        soma += resto
    x //= 10

Note that the operator // returns the result of the division already disregarding the decimal places, so no need to do (x - resto)//10. I also used soma += resto, who does the same as soma = soma + resto (for numbers the result will be the same, but there are cases where it makes a difference to use one or the other, understand better reading here and here). The same goes for x //= 10, which is the same as x = x // 10.

I also included a call to abs to return the absolute value of the number (no signal), so the algorithm works also for negative numbers (because -19 % 10 results in 1, not 9, so it is easier to turn the number to positive first and proceed from there). The code of the another answer does not take this case into account as it considers that all characters in the string will be digits, and when trying to convert the character - for int, make a mistake.

Maybe you think "but it is only an exercise, the user will always type positive numbers", But anyway, if I’m working with numbers, a mathematical algorithm usually has a better option than manipulating a string. So I prefer to convert the string to number only once, at the beginning of the algorithm, and if it is not a valid number, it already gives error right there and I don’t even start the algorithm (for example, if it is typed 123456789x, the other answer will go through all the digits, and only when you arrive at the x will give error, already my code will give error at the beginning, and nor will start the algorithm - it may seem a stupid detail, but the approach fail fast it seems more appropriate for this case - if the input is not a number, there is no reason to even start the algorithm).

Not to mention I can type things like +2 or +2 (with the sign of + and spaces at the beginning and end), which int can convert correctly to number (try using the code of the other answer, will give error because will try to convert the + or the space for number).

Maybe I’m being too rigid, but if the idea is for the user to enter a valid number, you have to be prepared to check if it’s actually a number, and just start the algorithm if it is. It’s not the focus of the question, but it would be something like:

try:
    x = abs(int(input('Número: ')))
    # restante do algoritmo...
except ValueError:
    print('Não foi digitado um número')

If you want, you can use divmod, so you have the result of division and the rest at once:

x = abs(int(input('Número: ')))
soma = 0
while x != 0:
    x, resto = divmod(x, 10)
    if resto % 2 != 0:
        soma += resto

0


In this question you can use the following algorithm...

n = input()

soma = 0
for c in n:
    x = int(c)
    if x % 2 != 0:
        soma += x
print(soma)

See how the algorithm works on repl it..

Note that in this algorithm the following facts are occurring:

  1. The algorithm captures the numerical value and stores it as string;
  2. The noose for runs through all the elements of string and converts them to whole;
  3. Checks if the whole is impar and, if so, makes the sum of them;
  4. Displays the soma of all odd digits.
  • 1

    Thank you, it worked perfectly as I wanted!

  • Ok! We’re here to help, with quality content.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.