Advertisement
Vladkoheca

Chetna / Nechetna suma.java

Oct 7th, 2024 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2. public class MyClass {
  3.   public static void main(String args[]) {
  4.     Scanner sc = new Scanner(System.in);
  5.     int n = sc.nextInt();
  6.    
  7.     int sumEven = 0;
  8.     int sumOdd = 0;
  9.     for (int i = 1; i <= n; i++) {
  10.         int x = sc.nextInt();
  11.        
  12.         if (i % 2 == 0) {
  13.             sumEven += x;
  14.         } else {
  15.             sumOdd += x;
  16.         }
  17.     }
  18.    
  19.     if (sumEven == sumOdd) {
  20.         System.out.println("Yes");
  21.         System.out.println("Sum = " + sumEven);
  22.     } else {
  23.         System.out.println("No");
  24.         System.out.println("Diff =" + Math.abs(sumEven - sumOdd));
  25.     }
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement