Advertisement
KeepCoding

Odd Even Sum

Nov 7th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class OddPositionVsEvenPositionSum {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(sc.nextLine());
  8. //        while (n < 2) { //gotta have at least one at odd and at even position
  9. //            n = Integer.parseInt(sc.nextLine());
  10. //        }
  11.  
  12.         int oddsSum = 0;
  13.         int evensSum = 0;
  14.  
  15.         for (int i = 1; i <= n; i++) {
  16.             if (i%2 == 1) { //i=1 is the first and at an odd position
  17.                 oddsSum += Integer.parseInt(sc.nextLine());
  18.             } else {
  19.                 evensSum += Integer.parseInt(sc.nextLine());
  20.             }
  21.         }
  22.  
  23.         if (Math.abs(evensSum - oddsSum) == 0) {
  24.             System.out.println("Yes");
  25.             System.out.println("Sum = " + evensSum);
  26.         } else {
  27.             System.out.println("No");
  28.             System.out.println("Diff = " + Math.abs(evensSum - oddsSum));
  29.         }
  30.  
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement