Advertisement
veronikaaa86

09. Left and Right Sum

Oct 1st, 2022
1,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. package forLoop;
  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.         for (int i = 1; i <= n; i++) {
  13.             int currentNum = Integer.parseInt(scanner.nextLine());
  14.  
  15.             leftSum = leftSum + currentNum;
  16.         }
  17.  
  18.         int rightSum = 0;
  19.         for (int i = 1; i <= n; i++) {
  20.             int currentNum = Integer.parseInt(scanner.nextLine());
  21.  
  22.             rightSum = rightSum + currentNum;
  23.         }
  24.  
  25. //        for (int i = 1; i <= 2 * n; i++) {
  26. //            int currentNum = Integer.parseInt(scanner.nextLine());
  27. //            if (i <= n) {
  28. //                leftSum = leftSum + currentNum;
  29. //            } else {
  30. //                rightSum = rightSum + currentNum;
  31. //            }
  32. //        }
  33.  
  34.         if (leftSum == rightSum) {
  35.             System.out.printf("Yes, sum = %d", leftSum);
  36.         } else {
  37.             int diff = Math.abs(leftSum - rightSum);
  38.             System.out.printf("No, diff = %d", diff);
  39.         }
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement