Varied amounts of input data

We Are Going To Discuss About Varied amounts of input data. So lets Start this Java Article.

Varied amounts of input data

Advertisements
  1. Varied amounts of input data

    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();

  2. Varied amounts of input data

    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();

Solution 1

Advertisements

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!

Original Author Of This Content

Solution 2

Advertisements
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);
   }
}

Original Author Of This Content

Solution 3

Advertisements
      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);
   }
}

Original Author Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment