Write a program that reads in 10 numbers Your program shoul

/* Write a program that reads in 10 numbers. Your program should do the following things:
       - Determine the number positive or negative
       - Count the number of positive and negative numbers
       - Output:
           --> the sum of all numbers greater than zero
           --> the sum of all numbers less than zero (which will be a negative number or zero)
           --> the summ lf ALL numbers (whether positive, negative, or zero)
       - Calculate the agerage of all the numbers   
   The user enters the 10 numbers just once each and the user can enter them in any order.
   Your program should NOT ask the user to enter the positive and negative numbers separately. */

Here\'s what I have so far...

#include <iostream>

using namespace std;

int main()
{
   int num, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
   int sum = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10);
   int avg = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10) / 10;

   if (num <= 0)
   {

   }

}

I\'m already confused on what to do. Is there a way to have the user enter in 10 integers (positive and negative) without declaring all the num1, num2, etc. (I know I need a cout and cin statement as well). And I need help wtih finishing it. I\'m also not sure on how to have the program detect if the number is greater than or less than zero (I\'m assuming an if statement according the lines of \"if(num <=0\" ). Please help and thanks!

Solution

import java.util.Scanner;

public class Calculation{

{

public static void main(String []args){
Scanner sc=new Scanner(System.in);
System.out.println(\"Enter the number of numbers you want to calculate :\" );

int length=sc.nextInt(); // for how many numbers you want to calculate like 10 or 20 so on.
  
int numbers[]=new int[length];
for(int i=0;i<length;i++){
numbers[i]=sc.nextInt();
}
  
int positiveCount=0;
int negativeCount=0;
int positiveSum=0;
int negativeSum=0;
int sumAll=0;
for(int i=0;i<length;i++){
if(numbers[i]>0){
positiveCount++;
positiveSum=positiveSum+numbers[i];
}else{
negativeCount++;
negativeSum=negativeSum+numbers[i];
}
sumAll=sumAll+numbers[i];
}
int average=sumAll/length;
System.out.println(\"Negative Count:\"+negativeCount);
System.out.println(\"positive Count:\"+positiveCount);
System.out.println(\"sum of numbers less than or equal to zero i.e Negative Sum:\"+negativeSum);
System.out.println(\"sum of numbers greater than zero i.e positive sum:\"+positiveSum);
System.out.println(\"Sum of all numbers is:\"+sumAll);
System.out.println(\"Average of numbers:\"+average);


}
}

/* Write a program that reads in 10 numbers. Your program should do the following things: - Determine the number positive or negative - Count the number of posi
/* Write a program that reads in 10 numbers. Your program should do the following things: - Determine the number positive or negative - Count the number of posi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site