Difference between sums of odd and even digits

13 Jun, 2022
Given a long integer, we need to find if the difference between sum of odd digits and sum of even digits is 0 or not. The indexes start from zero (0 index is for leftmost digit).Examples:  
Input : 1212112
Output : Yes
Explanation:-
the odd position element is 2+2+1=5
the even position element is 1+1+1+2=5
the difference is 5-5=0.so print yes.

Input :12345
Output : No
Explanation:-
the odd position element is 1+3+5=9
the even position element is 2+4=6
the difference is 9-6=3 not  equal
to zero. So print no.
 
Method 1: One by one traverse digits and find the two sums. If difference between two sums is 0, print yes, else no. Method 2 : This can be easily solved using divisibility of 11. This condition is only satisfied if the number is divisible by 11. So check the number is divisible by 11 or not.  
CPP
Java
Python
C#
PHP
Javascript
// C++ program to check if difference between sum of// odd digits and sum of even digits is 0 or not#include <bits/stdc++.h>using namespace std; bool isDiff0(long long int n){    return (n % 11 == 0);} int main() {         long int n = 1243    if (isDiff0(n))        cout << "Yes";    else        cout << "No";    return 0;}
// Java program to check if difference between sum of// odd digits and sum of even digits is 0 or not import java.io.*;import java.util.*; class GFG{    public static boolean isDiff(int n)    {        return (n % 11 == 0);    }    public static void main (String[] args)    {        int n = 1243;        if (isDiff(n))            System.out.print("Yes");        else            System.out.print("No");    }}
# Python program to check if difference between sum of# odd digits and sum of even digits is 0 or not def isDiff(n):    return (n % 11 == 0) # Driver coden = 1243;if (isDiff(n)):    print("Yes")else:    print("No") # Mohit Gupta_OMG <0_o>
// C# program to check if difference// between sum of odd digits and sum// of even digits is 0 or notusing System; class GFG {         public static bool isDiff(int n)    {        return (n % 11 == 0);    }         public static void Main ()    {        int n = 1243;                 if (isDiff(n))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }} // This code is contributed by vt_m.
<?php// PHP program to check if// difference between sum of// odd digits and sum of// even digits is 0 or not function isDiff0($n){    return ($n % 11 == 0);}     // Driver Code    $n = 1243;    if (isDiff0($n))        echo"Yes";    else        echo "No";         // This code is contributed by nitin mittal?>
<script> // Javascript program to check if difference between sum of// odd digits and sum of even digits is 0 or not function isDiff0(n){    return (n % 11 == 0);}          let n = 1243    if (isDiff0(n))        document.write("Yes");    else        document.write("No"); // This code is contributed by Mayank Tyagi </script>
Output: 
Yes
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by jaspal singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
nitin mittal
mayanktyagi1709
hasani
divisibility
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.