galinyotsev123

ProgBasics06For-Loop-P06leftAndRightSum

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