Answer:
The program to this question can be given as:
Program:
import java.util.*;
//import package for user input
class Main //define class
{
public static void main(String a[])
//define main function
{
int positive_number=0,negative_number=0,count=0,num; //define variable
double total=0,avg=0;
//creating Scanner class object.
Scanner ob = new Scanner(System.in);
System.out.println("Enter an integer, when done input 0: "); //message
num= ob.nextInt();
//taking input from user
if (num==0) //check number equal to 0
{
System.out.println("No numbers are entered except 0"); //message
System.exit(1);
}
else
{
while (num!= 0)
{
if (num> 0)
{
positive_number++; // Increase positives
}
else
{
negative_number++; // Increase negatives
}
total=total+num; // Accumulate total
count++; // Increase the count
num=ob.nextInt();
}
// Calculate the average
avg=total/count;
// Display values
System.out.println("The positive number is:"+positive_number);
System.out.println("The negatives number is:"+negative_number);
System.out.println("total is:"+total);
System.out.println("average is:"+avg);
}
}
}
Output:
Enter an integer, when done input 0: 22
2
1
4
0
The positive number is:4
The negatives number is:0
total is:29.0
average is:7.25
Explanation:
In the above program firstly we import the package for user input then we define a class that is main in this class we define the main method in the main method we define variable. Then we create a scanner class object for user input. In the number variable, we take multiple inputs from the user and also check that the user does not insert 0 at the starting of the program. To check this we use the condition statement that is a number equal to 0 then it will terminate the program. In the else part we first declare the loop that checks that inserted number is positive and negative and in this, we calculate the total of the numbers and at the end of the loop, we calculate the average of the number and print all the values.