Write a Java program that prompts the user to enter 10 real numbers and displays the arithmetic mean of positive and negative numbers.
3 -5 2 4 -3 1 8 -10 6 4
The average of negative numbers is -6 The average of positive numbers is 4
import java.util.Scanner; public class FindPositiveAndNegativeNumbers { public static void main(String[] args) { Scanner input = new Scanner(System.in); int total = 10; float[] numbers = new float[total]; float totalNegatives = 0.0f, totalPositives = 0.0f; int countNegatives = 0, countPositives = 0; for (int i = 0; i < total; i++) { numbers[i] = input.nextFloat(); } for (int i = 0; i < total; i++) { if (numbers[i] < 0) { totalNegatives += numbers[i]; countNegatives++; } if (numbers[i] > 0) { totalPositives += numbers[i]; countPositives++; } } if (countNegatives > 0) { float averageNegatives = totalNegatives / countNegatives; System.out.println("The average of negative numbers is " + averageNegatives); } else { System.out.println("No negative numbers were entered."); } if (countPositives > 0) { float averagePositives = totalPositives / countPositives; System.out.println("The average of positive numbers is " + averagePositives); } else { System.out.println("No positive numbers were entered."); } } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Create a Java program that prompts the user for 5 integers to store them in an integer array and displays them in reverse order.
Create a program in Java that serves to search an array of integer values.
Write a Java program that prompts the user to enter 10 integers. Then, store these numbers in an integer array and display only the even numbers.
Create a Java program that mimics the functionality of the banner command on Unix.
Create a Java program that prompts the user for 10 integers and sorts them using the bubble sort algorithm.
Create a Java program that prompts the user to input the scores of 10 students, divided into 2 groups of 5 students each.
Create a Java program that declares a two-dimensional character array of 20 rows by 70 columns and stores the letter 'X' to display on the screen.
Java Program for Mathematical Statistics: Store, Analyze, and Visualize Data. Enhance Your Skills!
Create a Java program that draws a 360 screen circumference.
Practice with exercises in Java that use arrays of different dimensions to store the information.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.