Advertisement
veronikaaa86

09. Left and Right Sum

May 29th, 2021
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 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 += 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 += currentNum;
  23.         }
  24.  
  25.         if (rightSum == leftSum) {
  26.             System.out.printf("Yes, sum = %d", leftSum);
  27.         } else {
  28.             System.out.printf("No, diff = %d", Math.abs(rightSum - leftSum));
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement