Saturday, October 11, 2025
HomeData Modelling & AIDifference between sums of odd and even digits

Difference between sums of odd and even digits

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 equal to zero.
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.

Approach:

One by one traverse digits and find the two sums. If difference between two sums is 0, print yes, else no. 

Below is the implementation of the above approach:

C++




// C++ program for above approach
#include<bits/stdc++.h>
using namespace std;
 
bool isDiff0(int n){
    int first = 0;
    int second = 0;
    bool flag = true;
    while(n > 0){
        int digit = n % 10;
        if(flag) first += digit;
        else second += digit;
        flag = !flag;
        n = n/10;
    }
    if(first - second == 0) return true;
    return false;
}
 
int main(){
    int n = 1243;
    if(isDiff0(n)) cout<<"Yes";
    else cout<<"No";
    return 0;
}
// This code is contributed by Kirti Agarwal(kirtiagarwal23121999)


Java




// Java equivalent of above code
public class Main {
    public static boolean isDiff0(int n) {
        int first = 0;
        int second = 0;
        boolean flag = true;
        while (n > 0) {
            int digit = n % 10;
            if (flag) first += digit;
            else second += digit;
            flag = !flag;
            n = n / 10;
        }
        if (first - second == 0) return true;
        return false;
    }
 
    public static void main(String[] args) {
        int n = 1243;
        if (isDiff0(n)) System.out.println("Yes");
        else System.out.println("No");
    }
}


Python




# Python program for the above approach
def isDiff0(n):
    first = 0
    second = 0
    flag = True
    while(n > 0):
        digit = n % 10
        if(flag):
            first += digit
        else:
            second += digit
        if(flag):
            flag = False
        else:
            flag = True
        n = int(n/10)
    if(first-second == 0):
        return True
    return False
 
 
# driver code
n = 1243
if(isDiff0(n)):
    print("Yes")
else:
    print("No")


C#




// C# Program for the above approach
using System;
 
public class BinaryTree{
    static bool isDiff0(int n){
        int first = 0;
        int second = 0;
        bool flag = true;
        while(n > 0){
            int digit = n % 10;
            if(flag) first += digit;
            else second += digit;
            flag = !flag;
            n = n/10;
        }
        if(first - second == 0) return true;
        return false;
    }
     
    public static void Main(){
        int n = 1243;
        if(isDiff0(n)) Console.Write("Yes");
        else Console.Write("No");
    }
}


Javascript




// JavaScript prgraom for the above approach
function isDiff0(n){
    let first = 0;
    let second = 0;
    let flag = true;
    while(n > 0){
        let digit = n % 10;
        if(flag) first += digit;
        else second += digit;
        flag = !flag;
        n = parseInt(n/10);
         
    }
    if(first - second == 0) return true;
    return false;
}
 
let n = 1243;
if(isDiff0(n))
    console.log("Yes");
else
    console.log("No");
     
    // THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGAWRAL2852002)


Output

Yes

Time Complexity: O(log n),
Auxiliary space: O(1)

This article is contributed by jaspal singh. If you like neveropen 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 neveropen 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.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32351 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11882 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7102 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS