Guest User

Untitled

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