yovkovbpfps

For Loop Left and Right Sum

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