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.
// 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"); } } |
chevron_right
filter_none
Output:
Yes
Please refer complete article on Difference between sums of odd and even digits for more details!
Recommended Posts:
- Java Program for Odd-Even Sort / Brick Sort
- Java Program to Check if count of divisors is even or odd
- Print even and odd numbers in increasing order using two threads in Java
- Binary string with given frequencies of sums of consecutive pairs of characters
- TCS Coding Practice Question | Check Odd or Even
- Java Program for Number of elements with odd factors in given range
- Java Program to Find the Number Occurring Odd Number of Times
- Java Program to Find sum of even factors of a number
- Java program to print Even length words in a String
- Java Program to Check if all digits of a number divide it
- Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Java Program for Sum the digits of a given number
- Program for Find sum of odd factors of a number
- Java ArrayList to print all possible words from phone digits
- How to check if string contains only digits in Java
- Difference between an Iterator and ListIterator in Java
- Java Program for Program to calculate area of a Tetrahedron
- Java Program for Program to find area of a circle
- Java Program for Program for array rotation
- Check if the given decimal number has 0 and 1 digits only

