46.8k views
1 vote
Varied amount of input data Statistics are often calculated with varying amounts of input data. Write a program that takes any number of integers as input, and outputs the average and max.

1 Answer

0 votes

Answer:

// Program will be written in Java Programming Language

// Comments are used for explanatory purpose

// Program starts here

import java.util.Scanner;

public class variednum

{

public static void main (String [} args)

{

Scanner input = new Scanner(System.in);

// Declare variables ; count, to count number of inputs; total to add all inputs

// And max to get the highest input.

// All declared Variables are initialised to 0

int count = 0, max = 0, total = 0;

// Prompt to ask if the user wants to input

System.out.print("Enter Y or y to continue: ");

char ask;

ask = input.next().charAt(0);

while ( ask == 'Y' || ask == 'y')

{

System.out.printn("Enter any number: ");

int num = input.nextInt();

// The above code segment gets input from the user

count++;

total += num;

// Check max number

max = Math.max(max, num);

System.out.println("Enter Y or y to continue: ");

ask = input.next().charAt(0);

}

// Calculate average

int avg = count == 0 ? 0 : total/count;

// Print average and max if count is not 0

System.out.println("Average = "+avg + "; Max = " + max);

}

}

User Fedeisas
by
4.5k points