Advertisement
veronikaaa86

10. Odd Even Sum

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