Sum of even digits in a given number in python

I am trying to find the difference between the sum of the even digits and odd digits in a number. My code only works for just one case which is 412 but it doesnt work for numbers such as 23 and 1203. I dont know why. Here is my code

def digitS(n):
    even = 0
    odd = 0
    while (n!=0):
        for i in range(n):
            if i % 2 == 0:
                even = even + (n%10)
            else:
                odd = odd + (n%10)
            n//=10

    return even - odd
n = 412
print(digitS(n))

Sum of even digits in a given number in python

Abilogos

4,4772 gold badges16 silver badges37 bronze badges

asked Nov 24, 2020 at 11:22

Sum of even digits in a given number in python

3

It is kinda obscure to me, that you want to find the difference between odd and even digits or the difference between odd and even positions?

BTW, I assumed you want to count the former -- your code implementing the latter with some issues in designing.

def digit(n):
   even = 0
   odd = 0
   while (n != 0):
       r = n % 10
       if r % 2 == 0:
          even = even + r
       else:
          odd = odd + r
       n //= 10

   return even - odd


n = 412
print(digit(int(n)))

and a more concise response with the aid of strings!

def digit(n):
   even = [int(i) if int(i) % 2 == 0 else 0 for i in str(n)]
   odd = [int(i) if int(i) % 2 else 0 for i in str(n)]
   return sum(even) - sum(odd)

answered Nov 24, 2020 at 11:40

javadrjavadr

3601 silver badge8 bronze badges

Just discard for loop.

def digitS(n):
    even = 0
    odd = 0
    while (n!=0):
        if n % 2 == 0:
            even += (n%10)
        else:
            odd += (n%10)
        n//=10

return even - odd
n = 412
print(digitS(n))

answered Nov 24, 2020 at 11:33

Sum of even digits in a given number in python

AbilogosAbilogos

4,4772 gold badges16 silver badges37 bronze badges

1

I've made some edits to your code below. What you have attempted is perfectly right, but the for loop is an obstruction and should be removed. Also, from what I understood, you wanted the difference between the odd and even digit sums. For that, you must return the absolute value of even - odd.

def digits(n):
    even = 0
    odd = 0

    while (n!=0):
        if n % 2 == 0:
            even += (n%10)
        else:
            odd += (n%10)
        n //= 10

    return abs(even - odd)

print(digits(412)) # 5
print(digits(23)) # 1

answered Nov 24, 2020 at 11:33

tanmay_gargtanmay_garg

3891 silver badge13 bronze badges

2

I think that you don't need for in 5th line, as you only want to iterate digits instead of whole range of numbers. So for example if number is 1203 you want to check 1, 2, 0 and 3, instead of 0, 1, 2, 3 ... 1201, 1202. Also checking digits is already handled in while(n!=0): n//=10, so for isn't needed.

answered Nov 24, 2020 at 11:34

Turn your input to a string, iterate over the elements one by one, check if they are odd or even by turning them to integers et Voila!

def digit(n):
    even = 0
    odd = 0
    string = str(n)
    for i in string:
        if int(i) % 2 == 0:
            even += int(i)
        else:
            odd += int(i)
    return even - odd
n = 282
print(digit(n)) #12

answered Nov 24, 2020 at 11:37

ombkombk

2,0281 gold badge3 silver badges15 bronze badges

I'm not sure what the for loop is meant to do. But here's an example of what you could do instead. Discard the for loop and go through each digit until n == 0.

def digit(n):
    even = 0 # Sum of even numbers
    odd = 0 # Sum of odd numbers
    while (n!=0):
        digit = n%10 # Get the right most digit of the number
        if digit % 2 == 0: # Check if the digit is even or odd
            even = even + digit
        else:
            odd = odd + digit
        n//=10 # Remove the digit from the number

    return even - odd
n = 412
print(digit(n))

answered Nov 24, 2020 at 11:39

Sum of even digits in a given number in python

SandstenSandsten

6873 silver badges16 bronze badges

How do you find the sum of Even Numbers in Python?

sum=0..
for i in range(15):.
if i%2==0:.
sum=sum+i..
print("sum =",sum).

How do you find the sum of odd digits in a number in Python?

Working :.
Step 1: Read the integer input..
Step 2: initialize s and set it to zero..
Step 3: using modulo with 10 logic we will get last digit of the number..
Step 4: Do this operation by using while loop..
Step 5: Check whether the number is odd or not , if number is odd add to the result..
Step 6: Print the resultant sum..

How do you find the even positioned digits of a number?

Approach: First, calculate the reverse of the given number. To the reverse number we apply modulus operator and extract its last digit which is actually the first digit of a number so it is odd positioned digit. The next digit will be even positioned digit, and we can take the sum in alternating turns.

How do you find the sum of a 3 digit number in Python?

Sum of Digits Program in Python.
Take the value of the integer and store in a variable..
Using a while loop, get each digit of the number and add the digits to a variable..
Print the sum of the digits of the number..