Advertisement
veronikaaa86

09. Left and Right Sum

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