Write a program that allows the user to enter an integer n, and then generates n
ID: 3832353 • Letter: W
Question
Write a program that allows the user to enter an integer n, and then generates n random numbers between 1 and 100. The program calculates and prints the sum, average, and all the number above the average. 1. Draw the flowchart for the program on the back of this paper. 2. Open TextPad and save the source file on the desktop using the file name as lastname_final 3. Write your name on the first line of the source file, and short comment for each task in the program. 4. Leave the TextPad open and this paper on the keyboard when you finish the exam. An output example: Format the average (as the output example) Find how many numbers are above the average (as the output example)Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class Chegg {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an Integer -> ");
int n = sc.nextInt();
int sum = 0;
int random[] = new int[n]; //In the array randon[] we will store the randomly generated number
Random ran = new Random();
System.out.println(" ");
for (int i = 0; i < n; i++) {
// In this loop we fill the array random[] with 'n' random number
random[i] = ran.nextInt(99) + 1;
/* The statement ran.nextInt(99) will generate a random Integer between 0 to 99.
As the question says the random number generated should be between 1 to 100, thats why 1 is added the to
randomly generated number so that it will come within the given range
*/
sum += random[i];// We are simaltaneously calculating the sum of the generated number
}
System.out.println(n + " random numbers are generated:");
for (int i = 0; i < n; i++) {
System.out.print(random[i] + " ");//Printing the randomly generated Integer
}
System.out.println(" ");
System.out.println("The sum of the random numbers is: " + sum + " ");//Printing the sum
double average = (double) sum / n;
System.out.println("The average of the random numbers is: " + average + " ");// We are printing the average here.
//We have converted sum from int to double because if we keep sum in int, the decimal part will not be printed
int count = 0;
int greater[] = new int[n];
for (int i = 0; i < n; i++) { // Printing number greater than average
if (random[i] > average) {
greater[count++] = random[i];
}
}
System.out.println("There are " + count + " numbers above the average :");
for (int i = 0; i < count; i++) {//Printing the number greater than average
System.out.print(greater[i] + " ");
}
System.out.println(" ");
}
}
_______________________________________________________________________________________
Sample OUTPUT of the above code
Enter an Integer -> 20
20 random numbers are generated:
43 92 77 93 5 9 73 39 43 87 56 96 17 9 34 31 40 99 26 3
The sum of the random numbers is: 972
The average of the random numbers is: 48.6
There are 8 numbers above the average :
92 77 93 73 87 56 96 99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.