Difference between sums of odd and even digits.

JavaObject Oriented ProgrammingProgramming

Problem Statement

With a given long integer n, write a program to find the difference between sum of the odd digits and even digits to be 0 or not. Index starts from 0.

Example

n = 1212112
Sum of odd position elements
= 2 + 2 + 1
= 5

Sum of even position elements
= 1 + 1 + 1 + 2
= 5

Difference
= 5 - 5
= 0

Output
= Yes

Example

 Live Demo

Following is the program in Java to find the required output.

class JavaTester {
   public static int difference(int n){
      return (n % 11);
   }
   public static void main(String args[]){
      int n = 1212112;
      System.out.println("Number: " + n);
      System.out.println(difference(n) == 0 ? "Yes" : "No");
      n = 12121121;
      System.out.println("Number: " + n);
      System.out.println(difference(n) == 0 ? "Yes" : "No");
   }
}

Output

Number : 1212112
Output: Yes
Number : 12121121
Output: No
raja
Published on 15-Apr-2020 11:02:43
Advertisements