Advertisement
veronikaaa86

09. Left and Right Sum

Oct 23rd, 2021
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 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. //left sum
  12. int leftSum = 0;
  13. for (int i = 0; i < n; i++) {
  14. int currentNum = Integer.parseInt(scanner.nextLine());
  15. leftSum += currentNum;
  16. }
  17.  
  18. //right sum
  19. int rightSum = 0;
  20. for (int i = 0; i < n; i++) {
  21. int currentNum = Integer.parseInt(scanner.nextLine());
  22. rightSum += currentNum;
  23. }
  24.  
  25. if (leftSum == rightSum) {
  26. System.out.printf("Yes, sum = %d%n", 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