Advertisement
IvaAnd

EvenAndOdd_10

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