# Python Program for Difference between sums of odd and even digits

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution and approach to solve the given problem statement.

Problem statement −Given an integer, we need to calculate if the difference between the sum of odd digits and sum of even digits is 0 or not.

The brute-force approach will be calculating the sum of all even and odd digits in the numbers and subtracting them to compute the answer.

To reduce the computational time we use the concept of mental mathematics .

The above constraints holds true only if the numbers are divisible by 11. So here in the implementation given below we check the divisibility of the number by 11.

Here the complexity reduces from O(n) to some contant amount of time involved in divibility and comparision..

Now let’s see the implementation −

## Example

Live Demo

def isDiff(n):
return (n % 11 == 0)
# main
n = 785643
if (isDiff(n)):
print("Yes")
else:
print("No")

## Output

No

All variables and functions are declared in global scope as shown in the figure below.

## Conclusion

In this article, we learned about the approach to compute the difference between sums of odd and even digits

Published on 25-Sep-2019 13:20:27