Advertisement
veronikaaa86

09. Left and Right Sum - with 1 loop

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