Advertisement
Venciity

[SoftUni Java] ExamPreparation 02.OddEvenSum

May 20th, 2014
270
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.  
  4. public class Program {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         int n = input.nextInt();
  9.         int sumOdd = 0;
  10.         int sumEven = 0;
  11.         boolean odd = true;
  12.        
  13.         for (int i = 0; i < 2 * n; i++) {
  14.             int element = new Scanner(System.in).nextInt();
  15.             if (odd) {
  16.                 sumOdd += element;
  17.             }
  18.             else {
  19.                 sumEven += element;
  20.             }
  21.             odd = !odd;
  22.         }
  23.        
  24.         if (sumOdd == sumEven) {
  25.             System.out.println("Yes, sum=" +  sumOdd);
  26.         } else {
  27.             int diff = sumEven - sumOdd;
  28.             if (diff < 0) {
  29.                 diff *= -1;
  30.             }
  31.             System.out.println("No, diff=" + diff);
  32.         }
  33.  
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement