Write a Java program that will read an unspecified number of integers and then print number of positives, number of negatives, their count, sum and average. Your program should stop reading numbers when the user enter 0. Name your class CountPosNeg
All Answers
need an explanation for this answer? contact us directly to get an explanation for this answer
need an explanation for this answer? contact us directly to get an explanation for this answer
need an explanation for this answer? contact us directly to get an explanation for this answer
import java.util.Scanner;
public class CountPosNeg {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int x, pos=0, neg=0, sum = 0, count=0;
double avg;
System.out.print("Enter integers ending with 0: ");
x=kb.nextInt();
while (x!=0) {
count++;
sum+=x;
if (x>=0) pos++; else neg++;
x = kb.nextInt();
}
avg = (double) sum / count;
System.out.println("The number of positives is "+pos);
System.out.println("The number of negativess is "+neg);
System.out.println("The count "+count);
System.out.println("The sum "+sum);
System.out.println("The average "+avg);
kb.close();
}
}
need an explanation for this answer? contact us directly to get an explanation for this answer
total answers (2)