desislava_topuzakova

09. Left and Right Sum

Oct 2nd, 2021
1,666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class LeftRightSum {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.         int sum1 = 0;
  9.         int sum2 = 0;
  10.  
  11.         for (int i = 1; i <= n; i++) {
  12.             int number1 = Integer.parseInt(scanner.nextLine());
  13.             sum1 = sum1 + number1;
  14.         }
  15.  
  16.         for (int i = 1; i <= n; i++) {
  17.             int number2 = Integer.parseInt(scanner.nextLine());
  18.             sum2 = sum2 + number2;
  19.         }
  20.  
  21.         if (sum1 == sum2) {
  22.  
  23.             System.out.println("Yes, sum = " + sum1);
  24.         }else  {
  25.             int diff = Math.abs(sum1 - sum2);
  26.  
  27.             System.out.println("No, diff = " + diff);
  28.         }
  29.  
  30.  
  31.  
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment