Advertisement
veronikaaa86

09. Left and Right Sum

Jul 23rd, 2022
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. package forLoop;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5.  
  6. public class P09LeftAndRightSum {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. int n = Integer.parseInt(scanner.nextLine());
  11.  
  12. int leftSum = 0;
  13. for (int i = 1; i <= n; i++) {
  14. int currentNum = Integer.parseInt(scanner.nextLine());
  15.  
  16. leftSum = leftSum + currentNum;
  17. }
  18.  
  19. int rightSum = 0;
  20. for (int i = 1; i <= n; i++) {
  21. int currentNum = Integer.parseInt(scanner.nextLine());
  22.  
  23. rightSum = rightSum + currentNum;
  24. }
  25.  
  26. // int leftSum = 0;
  27. // int rightSum = 0;
  28. // for (int i = 1; i <= 2 * n; i++) {
  29. // int currentNum = Integer.parseInt(scanner.nextLine());
  30. // if (i <= n) {
  31. // leftSum += currentNum;
  32. // } else {
  33. // rightSum += currentNum;
  34. // }
  35. // }
  36.  
  37. if (leftSum == rightSum) {
  38. System.out.printf("Yes, sum = %d", leftSum);
  39. } else {
  40. int diff = Math.abs(leftSum - rightSum);
  41. System.out.printf("No, diff = %d", diff);
  42. }
  43. }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement