Advertisement
veronikaaa86

05. Even and Odd Subtraction

Sep 30th, 2021
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. package arrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P05EvenAndOddSubtraction {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String[] arr = scanner.nextLine().split(" ");
  10.  
  11. int[] numArr = new int[arr.length];
  12. for (int i = 0; i < numArr.length; i++) {
  13. numArr[i] = Integer.parseInt(arr[i]);
  14. }
  15.  
  16. int evenSum = 0;
  17. int oddSum = 0;
  18. for (int num : numArr) {
  19. if (num % 2 == 0) {
  20. evenSum += num;
  21. } else {
  22. oddSum += num;
  23. }
  24. }
  25.  
  26. // for (int i = 0; i < numArr.length; i++) {
  27. // if (numArr[i] % 2 == 0) {
  28. // evenSum += numArr[i];
  29. // } else {
  30. // oddSum += numArr[i];
  31. // }
  32. // }
  33.  
  34. int diff = evenSum - oddSum;
  35.  
  36. System.out.println(diff);
  37. }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement