Advertisement
mark79

Equal Sums Even Odd Position

Oct 28th, 2019
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class EqualSumsEvenOddPosition {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int a = Integer.parseInt(scanner.nextLine());
  8.         int b = Integer.parseInt(scanner.nextLine());
  9.  
  10.         for (int i = a; i <= b; i++) {
  11.             String currentNumber = Integer.toString(i);
  12.  
  13.             int evenSum = 0;
  14.             int oddSum = 0;
  15.  
  16.             for (int j = 0; j < currentNumber.length(); j++) {
  17.                 if ((j + 1) % 2 == 0) {
  18.                     evenSum += Character.getNumericValue(currentNumber.charAt(j));
  19.                 } else {
  20.                     oddSum += Character.getNumericValue(currentNumber.charAt(j));
  21.                 }
  22.             }
  23.  
  24.             if (evenSum == oddSum) {
  25.                 System.out.printf("%d ", i);
  26.             }
  27.  
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement