Advertisement
dimipan80

C#Exams 2. Odd / Even Sum (on Java Code)

Aug 21st, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _2_Odd_EvenSum {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.         Scanner scan = new Scanner(System.in);
  8.         int count = scan.nextInt();
  9.  
  10.         boolean numIsOdd = true;
  11.         long oddSum = 0;
  12.         long evenSum = 0;
  13.         for (int i = 0; i < 2 * count; i++) {
  14.             if (numIsOdd) {
  15.                 oddSum += scan.nextInt();
  16.             } else {
  17.                 evenSum += scan.nextInt();
  18.             }
  19.  
  20.             numIsOdd = !numIsOdd;
  21.         }
  22.  
  23.         long difference = Math.abs(oddSum - evenSum);
  24.         if (difference == 0) {
  25.             System.out.println("Yes, sum=" + oddSum);
  26.         } else {
  27.             System.out.println("No, diff=" + difference);
  28.         }
  29.     }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement