skipter

Odd Even Sum - Java Basics - Loops

Apr 5th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) throws InterruptedException {
  5.         Scanner input = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(input.nextLine());
  8.  
  9.         int leftSum = 0;
  10.         int rightSum = 0;
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             int currentNumber = Integer.parseInt(input.nextLine());
  14.             if (i % 2 == 0) {
  15.                 leftSum += currentNumber;
  16.             } else {
  17.                 rightSum += currentNumber;
  18.             }
  19.  
  20.         }
  21.         int diff = Math.abs(leftSum - rightSum);
  22.         if (diff != 0) {
  23.             System.out.println("No diff = " + diff);
  24.         } else {
  25.             System.out.println("Yes Sum = " + leftSum);
  26.         }
  27.  
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment