Advertisement
desislava_topuzakova

09. Left and Right Sum

May 8th, 2021
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class LeftAndRightSum {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         int n = Integer.parseInt(scan.nextLine());
  7.         int leftSum = 0;
  8.         int rightSum = 0;
  9.         for (int i = 0; i < n; i++) {
  10.             int n1 = Integer.parseInt(scan.nextLine());
  11.             leftSum = leftSum + n1;
  12.         }
  13.         for (int i = 0; i < n; i++) {
  14.             int n2 = Integer.parseInt(scan.nextLine());
  15.             rightSum = rightSum + n2;
  16.         }
  17.         if (leftSum == rightSum) {
  18.             System.out.printf("Yes, sum = %d", leftSum);
  19.         } else {
  20.             System.out.printf("No, diff = %d", Math.abs(leftSum - rightSum));
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement