Advertisement
Spocoman

10. Odd Even Sum

Aug 27th, 2024 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class OddEvenSum {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.  
  8.         int oddSum = 0, evenSum = 0;
  9.  
  10.         for (int i = 1; i <= n; i++) {
  11.             int num = Integer.parseInt(scanner.nextLine());
  12.             if (i % 2 == 1) {
  13.                 oddSum += num;
  14.             } else {
  15.                 evenSum += num;
  16.             }
  17.         }
  18.  
  19.         if (oddSum == evenSum) {
  20.             System.out.printf("Yes\nSum = %d\n", oddSum);
  21.         } else {
  22.             System.out.printf("No\nDiff = %d\n", Math.abs(oddSum - evenSum));
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement