203k views
1 vote
Write a program that computes the average of a list of numbers entered by the user. The numbers are supposed to be in the range 50 – 100 (inclusive) and the user will indicate that he/she is done with data input by entering a value not in that range. At least one number will be in the range. Use a WHILE or a DO-WHILE loop.

1 Answer

4 votes

Answer:

The answer to this question can be given as:

Program:

//import package.

import java.util.*;

public class Main //define the main class.

{

public static void main(String[] ar) //define main method.

{

int sum=0,avg=0,count=0,data; //define varaible

// Create a Scanner class object.

Scanner ob1 = new Scanner(System.in);

System.out.println( "enter number between 50 - 100 After done please insert 0:"); //message

data = ob1.nextInt(); //input by user.

while (data >50 && data<100) //number between 50 to 100

{

if(data!=0) //condition

{

sum =sum+ data; //add numbers.

data = ob1.nextInt(); //next input

count++; //count number.

}

}

System.out.println("The sum is :" + sum); //print sum.

avg=sum/count;

System.out.print("The average is :" + avg); //print average.

}

}

Output:

enter number between 50 - 100 After done please insert 0:

80

89

90

96

66

89

0

The sum is :510

The average is :85

Explanation:

In the above program firstly we import java util package. This package is used to take input from the user. Then we declare the class that is main in this class we define the main function in the main function we define the variable that is sum, avg, count, and data. In the data variable, we take the input from the user. Then we define the while loop in this loop we define condition that number is greater then 50 and less than 100.in this loop we define if block in the if block we check that the number is not equal to 0. so it will sum all the elements and count the number inserted by the user. In the last, it will print the sum of the number and the average of the number.

User Lsavio
by
7.7k points