Advertisement
veronikaaa86

09. Left and Right Sum

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