Advertisement
IvoSavov

Odd / Even Positions

Apr 10th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. package tasks;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Task_02 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         System.out.println("n = ");
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.         System.out.println("Enter the numbers: ");
  11.  
  12.         double oddSum = 0.00;
  13.         double oddMin = 1000000000.0;
  14.         double oddMax = -1000000000.0;
  15.         double evenSum = 0.00;
  16.         double evenMin = 1000000000.0;
  17.         double evenMax = -1000000000.0;
  18.  
  19.         for (int i = 1; i <= n; i++) {
  20.             double num = Double.parseDouble(scanner.nextLine());
  21.             if (i % 2 != 0) {
  22.                 oddSum += num;
  23.                 if (num < oddMin) {
  24.                     oddMin = num;
  25.                 }
  26.                 if (num > oddMax) {
  27.                     oddMax = num;
  28.                 }
  29.             } else {
  30.                 evenSum += num;
  31.                 if (num < evenMin) {
  32.                     evenMin = num;
  33.                 }
  34.                 if (num > evenMax) {
  35.                     evenMax = num;
  36.                 }
  37.             }
  38.         }
  39.         if (oddMin == 1000000000.0) {
  40.             System.out.printf("OddSum=%.0f, %nOddMin=No, %nOddMax=No,", oddSum);
  41.         } else if (oddSum % 1 == 0) {
  42.             System.out.printf("OddSum=%.0f, %nOddMin=%.0f, %nOddMax=%.0f,",
  43.                     oddSum, oddMin, oddMax);
  44.         } else {
  45.             System.out.printf("OddSum=%.1f, %nOddMin=%.1f, %nOddMax=%.1f,",
  46.                     oddSum, oddMin, oddMax);
  47.         }
  48.         if (evenMin == 1000000000.0) {
  49.             System.out.printf("%nEvenSum=%.0f, %nEvenMin=No, %nEvenMax=No", evenSum);
  50.         } else if (evenSum % 1 == 0) {
  51.             System.out.printf("%nEvenSum=%.0f, %nEvenMin=%.0f, %nEvenMax=%.0f", evenSum, evenMin, evenMax);
  52.         } else {
  53.             System.out.printf("%nEvenSum=%.1f %nEvenMin=%.1f, %nEvenMax=%.1f", evenSum, evenMin, evenMax);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement