Write a Java program that will input integer values from the

Write a Java program that will input integer values from the user. The user will enter a sequence of integer values in a loop that will go for maximum 10 iterations (declare a class constant for 10) but it will stop if the user is entering a negative value. The program computes the sum and the average of all positive numbers in the sequence. HINT: You will need to implement a loop that is both count-controlled (has up to 10 iterations) and event-controlled (stops earlier if a negative value is entered).

Solution

class SumAverage

{

public static void main(String args[])

{ int count = 10,n;

int ar[] = new Array();

boolean flag;

Scanner s = new Scanner(System.in);

for(int i=1;i<=count;i++)

{ n= s.nextInt();

if(n<=0)

{System.out.println(\"You have entered negative value\");

flag = false;

break;}

ar[i] = n;

}

if(flag)

{

for(int i = 1;i<=10;i++)

{

sum = sum+ar[i];

}

average = sum /count;

System.out.println(\"Sum of entered numbers is\" + sum);

System.out.println(\"Avergae of entered numbers is\" + average);

}

}

}

Write a Java program that will input integer values from the user. The user will enter a sequence of integer values in a loop that will go for maximum 10 iterat

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site