Advertisement
galinyotsev123

ProgBasics07Nested-Loops-Y06equalSumsLeftRightPosition2

Feb 2nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int firstNumber = Integer.parseInt(scanner.nextLine());
  8. int secondNumber = Integer.parseInt(scanner.nextLine());
  9.  
  10. int sumRightNumbers = 0;
  11. int sumLeftNumbers = 0;
  12.  
  13. for (int i = firstNumber; i <= secondNumber; i++) {
  14. int currentNum = i;
  15.  
  16. int first = currentNum % 10;
  17. currentNum /= 10;
  18. int second = currentNum % 10;
  19. currentNum /= 10;
  20. int third = currentNum % 10;
  21. currentNum /= 10;
  22.  
  23. sumRightNumbers = first + second;
  24.  
  25. int fourth = currentNum % 10;
  26. currentNum /= 10;
  27. int fifth = currentNum % 10;
  28. currentNum /= 10;
  29.  
  30. sumLeftNumbers = fourth + fifth;
  31.  
  32.  
  33. if (sumLeftNumbers == sumRightNumbers) {
  34. System.out.printf("%d ", i); // or System.out.print(i + " " );
  35. } else if (sumLeftNumbers > sumRightNumbers){
  36. sumRightNumbers += third;
  37. if (sumLeftNumbers == sumRightNumbers) {
  38. System.out.printf("%d ", i); // or System.out.print(i + " " );
  39. }
  40. } else if (sumLeftNumbers < sumRightNumbers) {
  41. sumLeftNumbers += third;
  42. if (sumLeftNumbers == sumRightNumbers) {
  43. System.out.printf("%d ", i); // or System.out.print(i + " " );
  44. }
  45. }
  46. }
  47.  
  48.  
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement