Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Difference between Sums of Odd and Even Digits in Python
Next article icon

Difference between Sums of Odd and Even Digits in Python

Last Updated : 20 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a python program for a given 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 the 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 the difference between two sums is 0, print yes, else 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") 

Output
Yes 

Time Complexity: O(n), where n is the number of digits in the input number.
Auxiliary Space: O(1)

Please refer complete article on Difference between sums of odd and even digits for more details!


Next Article
Difference between Sums of Odd and Even Digits in Python

K

kartik
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

    Find Average of a Number Digits in Python
    AIn this article, we will see how to find the average of the digits of a number in Python. Examples: Input: N = 642 Output: 4.0 Explanation: (6+4+2)/3 = 12/3 = 4.0 Input: N = 3504 Output: 3.0 Explanation: (3+5+0+4)/4 = 12/4 = 3.0Calculate the average of a number of digits in python if the number is
    3 min read
    Sum Even and Odd Values with One For-Loop and No If-Condition Using Python
    Summing even and odd numbers in a list is a common programming task. Typically, we'd use an if condition to check whether a number is even or odd. However, we can achieve this in Python efficiently using arithmetic operators and list slicing, without relying on if conditions. In this article, we'll
    4 min read
    Sort Even-Placed in Increasing and Odd-placed Elements in Decreasing Order - Python
    We need to sort the elements at even indices in increasing order and the elements at odd indices in decreasing order. For example, given a list [10, 3, 5, 8, 2, 7, 6, 1], the result should be [2, 8, 5, 7, 6, 3, 10, 1]. Let us explore different ways to achieve this in Python.Using List Comprehension
    3 min read
    SymPy | Permutation.is_even() in Python
    Permutation.is_even() : is_even() is a sympy Python library function that checks whether the permutation is even. Syntax : sympy.combinatorics.permutations.Permutation.is_even() Return : true - if the permutation is even; otherwise false Code #1 : is_even() Example Python3 1=1 # Python code explaini
    1 min read
    Filter Even Values from a List - Python
    The task of filtering even values from a list in Python involves selecting elements from the list that are even numbers. For example, given the list a = [1, 2, 3, 4, 5], the goal is to filter out the even values, resulting in a new list like this [2, 4].Using list comprehensionList comprehension pro
    2 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