Varied Amounts of Input Data

Problem: Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics.

Ex: When the input is: 15 20 0 5 -1

the output is: 10 20

You can assume that at least one non-negative integer is input.

import java.util.Scanner; 

public class LabProgram {
   public static void main(String[] args) {

      Scanner scnr = new Scanner (System.in);
      int num = 0;
      int count = 0;
      int max = 0;
      int total = 0;
      int avg = 0;

     do {

         total += num;
         num = scnr.nextInt();
         count = ++count;


         if (num >= max) {
            max = num;
         }

      } while (num >= 0);

     avg = total/(count-1);

      System.out.println(avg + " " + max);

   }
}

I had a lot of trouble with this problem. Is there any way I could have done this without having to do count -1 while computing the average? Also, is this this the most efficient way I could have done it?

3

3 Answers

How about this? If you have questions from the implementation, please ask.

    public static void main(String[] args) throws IOException {
        Scanner scnr = new Scanner(System.in);
        int count = 0, max = 0, total = 0;

        int num = scnr.nextInt();
        while (num >= 0) {
            count++;
            total += num;
            max = Math.max(max, num);
            num = scnr.nextInt();
        }

        int avg = count == 0 ? 0 : total/count;
        System.out.println(avg + " " + max);
    }

If you use while loop instead of do-while loop, you don't have to count the negative number input anymore. And no, it's not the most efficient way, but it's a good start!

3
import java.util.Scanner; 

public class LabProgram {
   public static void main(String[] args) {

      Scanner scnr = new Scanner (System.in);
      int userNum;
      int maxNum = 0;
      int totalSum = 0;
      int averageNum;
      int count = 0;

      userNum = scnr.nextInt();

      while (userNum >= 0) {

         if (userNum > maxNum) {
            maxNum = userNum;
         }

         totalSum += userNum;
         ++count;

         userNum = scnr.nextInt();
      }
      averageNum = totalSum / count;

      System.out.println("" + averageNum + " " + maxNum);
   }
}

      int Num;
      int max = 0;
      int total = 0;
      int average;
      int count = 0;

      Num = scnr.nextInt();

      while (Num >= 0) {

         if (Num > max) {
            max = Num;
         }

         total += Num;
         ++count;

         Num = scnr.nextInt();
      }
      average = total / count;

      System.out.println("" + average + " " + max);
   }
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest