Question

/*Write a Java program that generates 1000 random integers, * calculates the average and prints two...

/*Write a Java program that generates 1000 random integers, * calculates the average and prints two counts, one for the * numbers lower than the average and one for the numbers larger than the average. */

Use java thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN JAVA:

import java.util.Random;
class RandomNumbers
{
public static void main(String args[])
{
Random r=new Random(); //creating object of Random class to use nextInt() to generate integers
final int BOUND=1000; //random integers will be in the range 0 to BOUND
int arr[]=new int[1000]; //creating empty array of size 1000
double sum=0; //to store sum of elements
for(int i=0;i<1000;i++) //filling the array and calculating the sum of elements
{
arr[i]=r.nextInt(BOUND+1);
sum+=arr[i];
}
double avg=sum/1000; //calculating average of elements
int above=0,below=0; //to store two counts, above average and below average
for(int i=0;i<1000;i++) //counting above average and below average
{
if(arr[i]>avg)
above++;
else if(arr[i]<avg)
below++;
}
//output
System.out.println(avg+" is the average");
System.out.println(above+" numbers were above average");
System.out.println(below+" numbers were below average");
}
}

OUTPUT:

2 Blue): Terminal Window - Chegg Options 478.61 is the average 483 numbers were above average 517 numbers were below average

Add a comment
Know the answer?
Add Answer to:
/*Write a Java program that generates 1000 random integers, * calculates the average and prints two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT