myrdok123

09. Left and Right Sum

Jun 8th, 2024
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. package ForLoop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class OddEvenSum_09 {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int countNumbers = Integer.parseInt(scanner.nextLine());
  11.  
  12.         int sumLeft = 0;
  13.         int sumRight = 0;
  14.  
  15.         for (int i = 1; i <= countNumbers * 2 ; i++) {
  16.  
  17.             int currentNumber = Integer.parseInt(scanner.nextLine());
  18.  
  19.             if(i <= countNumbers){
  20.                 sumLeft += currentNumber;
  21.             }else {
  22.                 sumRight += currentNumber;
  23.             }
  24.         }
  25.  
  26.         if (sumLeft == sumRight){
  27.             System.out.printf("Yes, sum = %d", sumLeft);
  28.         }else {
  29.             int diff = Math.abs(sumLeft - sumRight);
  30.             System.out.printf("No, diff = %d", diff);
  31.         }
  32.     }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment