Advertisement
veronikaaa86

10. Odd Even Sum

Oct 23rd, 2021
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 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 evenSum = 0;
  12. int oddSum = 0;
  13. for (int i = 1; i <= n; i++) {
  14. int currentNum = Integer.parseInt(scanner.nextLine());
  15.  
  16. if (i % 2 == 0) {
  17. evenSum += currentNum;
  18. } else {
  19. oddSum += currentNum;
  20. }
  21. }
  22.  
  23. if (oddSum == evenSum) {
  24. System.out.printf("Yes%nSum = %d%n", oddSum);
  25. } else {
  26. System.out.printf("No%nDiff = %d%n", Math.abs(oddSum - evenSum));
  27. }
  28. }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement