Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Difference between sum of odd and even frequent elements in an Array
Next article icon

Difference between sums of odd and even digits

Last Updated : 20 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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.

Recommended Practice
Difference between sums of odd and even digits
Try It!

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)



 



Next Article
Difference between sum of odd and even frequent elements in an Array
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • DSA
  • Mathematical
  • Misc
  • divisibility
Practice Tags :
  • Mathematical
  • Misc

Similar Reads

  • Difference between sum of K maximum even and odd array elements
    Given an array arr[] and a number K, the task is to find the absolute difference of the sum of K maximum even and odd array elements.Note: At least K even and odd elements are present in the array respectively. Examples: Input arr[] = {1, 2, 3, 4, 5, 6}, K = 2Output: 2Explanation:The 2 maximum even
    8 min read
  • Difference between sum of odd and even frequent elements in an Array
    Given an array arr[] of integers, the task is to find the absolute difference between the sum of all odd frequent array elements and the sum of all even frequent array elements. Examples: Input: arr[] = {1, 5, 5, 2, 4, 3, 3} Output: 9 Explanation: The even frequent elements are 5 and 3 (both occurri
    12 min read
  • Difference between sums of odd level and even level nodes in an N-ary Tree
    Given an N-ary Tree rooted at 1, the task is to find the difference between the sum of nodes at the odd level and the sum of nodes at even level. Examples: Input: 4 / | \ 2 3 -5 / \ / \ -1 3 -2 6Output: 10Explanation:Sum of nodes at even levels = 2 + 3 + (-5) = 0Sum of nodes at odd levels = 4 + (-1)
    9 min read
  • Maximum difference between sum of even and odd indexed elements of a Subarray
    Given an array nums[] of size N, the task is to find the maximum difference between the sum of even and odd indexed elements of a subarray.  Examples: Input: nums[] = {1, 2, 3, 4, -5}Output: 9Explanation: If we select the subarray {4, -5} the sum of even indexed elements is 4 and odd indexed element
    11 min read
  • Difference between sums of odd level and even level nodes of a Binary Tree
    Given a Binary Tree, the task is to find the difference between the sum of nodes at the odd level and the sum of nodes at the even level. Examples: Input: Output: -4Explanation: sum at odd levels - sum at even levels = (1) - (2 + 3) = 1 - 5 = -4Input: Output: 60Explanation: Sum at odd levels - Sum a
    14 min read
  • Difference between sum of even and odd valued nodes in a Binary Tree
    Given a binary tree, the task is to find the absolute difference between the even valued and the odd valued nodes in a binary tree. Examples: Input: 5 / \ 2 6 / \ \ 1 4 8 / / \ 3 7 9 Output: 5 Explanation: Sum of the odd value nodes is: 5 + 1 + 3 + 7 + 9 = 25 Sum of the even value nodes is: 2 + 6 +
    10 min read
  • Maximize difference between sum of even and odd-indexed elements of a subsequence
    Given an array arr[] consisting of N positive integers, the task is to find the maximum possible difference between the sum of even and odd-indexed elements of a subsequence from the given array. Examples: Input: arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9 } Output: 15 Explanation: Considering the subseq
    7 min read
  • Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
    Given a range [l, r]. The task is to count the numbers in the range having difference between the sum of digits at even position and sum of digits at odd position is a Prime Number. Consider the position of least significant digit in the number as an odd position. Examples: Input : l = 1, r = 50Outp
    15+ min read
  • Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
    Prerequisites: Digit DP Given a range [L, R], the task is to count the numbers in this range having the difference between, the sum of digits at even positions and sum of digits at odd positions, as a Fibonacci Number. Note: Consider the position of the least significant digit in the number as an od
    15 min read
  • Maximize difference between sum of even and odd-indexed elements of a subsequence | Set 2
    Given an array arr[] consisting of N positive integers, the task is to find the maximum value of the difference between the sum of elements at even and odd indices for any subsequence of the array. Note: The value of N is always greater than 1. Examples: Input: arr[] = { 3, 2, 1, 4, 5, 2, 1, 7, 8, 9
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences