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

This program contains if statements and possibly loops as well. Book exercise 4.

ID: 3620111 • Letter: T

Question

This program contains if statements and possibly loops as well.

Book exercise 4.1: (Counting positive and negative numbers and computing the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a decimal number. Here is a sample run:
Enter an int value, the program exits if the input is 0:
1 2 -1 3 0
The number of positives is 3 The number of negatives is 1 The total is 5 The average is 1.25

Explanation / Answer

please rate - thanks import java.util.*;
public class main
{
public static void main(String args[])
{int num,plus=0,minus=0,sum=0,i=0;
double avg;
Scanner in =new Scanner(System.in);
System.out.print("Enter an int value, the program exits if the input is 0: ");
num=in.nextInt();
while(num!=0)
   {i++;
   sum+=num;
   if(num>0)
        plus++;
   else if(num<0)
         minus++;
   System.out.print("Enter an int value, the program exits if the input is 0: ");
   num=in.nextInt();
   }
   avg=(double)sum/i;
System.out.println("The number of positives is "+plus);
System.out.println("The number of negatives is "+minus);
System.out.println("The total is "+sum+" The average is "+avg );
}
}
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote