Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class EqualSumsLeftRightPosition {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int firstNum = Integer.parseInt(scanner.nextLine());
  10. int secondNum = Integer.parseInt(scanner.nextLine());
  11.  
  12.  
  13. for (int i = firstNum; i <= secondNum; i++) {
  14. String currentDigit = "" + i;
  15.  
  16. int leftSum = 0;
  17. int rightSum = 0;
  18. int middleDigit = 0;
  19.  
  20.  
  21. for (int j = 0; j < currentDigit.length(); j++) {
  22. int currentNum = Integer.parseInt("" + currentDigit.charAt(j));
  23.  
  24. if (j <= 2) {
  25. leftSum += currentNum;
  26. } else if (j >= 4) {
  27. rightSum += currentNum;
  28. } else {
  29. middleDigit = currentNum;
  30. }
  31.  
  32. if (leftSum == rightSum) {
  33. System.out.print(i + " ");
  34. } else if (leftSum > rightSum) {
  35. rightSum += middleDigit;
  36. } else {
  37. leftSum += middleDigit;
  38. }
  39. }
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement