skipter

Left and Right Sum - Java Basics Loops

Apr 5th, 2017
228
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 Main {
  4.     public static void main(String[] args) throws InterruptedException {
  5.         Scanner input = new Scanner(System.in);
  6.         int n = Integer.parseInt(input.nextLine());
  7.         int leftSum = 0;
  8.         int rightSum = 0;
  9.  
  10.         for (int i = 0; i < n; i++) {
  11.             int left = Integer.parseInt(input.nextLine());
  12.              leftSum += left;
  13.         }
  14.  
  15.         for (int i = 0; i < n; i++) {
  16.             int right = Integer.parseInt(input.nextLine());
  17.             rightSum += right;
  18.         }
  19.         int diff = Math.abs(leftSum - rightSum);
  20.         if (!(diff == 0)) {
  21.             System.out.println("No, diff = " + diff);
  22.         } else {
  23.             System.out.println("Yes, sum = " + leftSum);
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment