Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Submit a .java program via canvas that accomplishes the following tasks: Your pr

ID: 640384 • Letter: S

Question

Submit a .java program via canvas that accomplishes the following tasks:

Your program should prompt the user to enter positive integers (>=0) and enter -1 to quit.

Your program should take the input and do the following:

If the input is a negative integer and NOT -1 you should print an error message to the user and go back to accepting positive integers or -1. That is, you don't quit but you don't use the negative integer in the following computations.

You should keep two running sums, one for even integers and one for odd integers. If a user enters an EVEN integer add it to the even sum. If the user enters an ODD integer add it to the odd sum.

If the user enters -1 you should stop taking input and print out two results. The average of the even integers the user entered and the average of the odd integers the user entered. Note, -1 should only be used to stop taking input and should not be part of either of the sums or averages.

The averages should be printed as doubles and contain 2 decimal positions.

Make sure you tell the user, in your output, which is the average for the even integers and which is the average for the odd integers.

Example:

Suppose the user entered the following sequence:

1

4

5

7

2

-1

Then your program should produce the following:

The average of the even integers is: 3.00

The average of the odd integers is: 4.33

Explanation / Answer

import java.util.Scanner;
public class HelloWorld{

public static void main(String []args){
System.out.println("Hello World");
Scanner sc=new Scanner(System.in);
int odd_sum=0,even_sum=0,num,even=0,odd=0;
while(true){
System.out.println("Enter number or -1 to exit");
num=sc.nextInt();
if(num!=-1){
if(num%2==0){
even_sum=even_sum+num;
even++;
}
else{
odd_sum=odd_sum+num;
odd++;
}
}
else{
System.out.println("odd average---->"+odd_sum/odd);
System.out.println("even average--->"+even_sum/even);
break;
}
}
}
}