Write a program that allows the user to degree enter an integer n, and then gene
ID: 3832352 • Letter: W
Question
Write a program that allows the user to degree 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:Explanation / Answer
package org.students;
import java.util.Random;
import java.util.Scanner;
public class RandomNumbersAvg {
public static void main(String args[]) {
//Declaring variables
int n,num,count=0,avgCnt=0;
int sum=0;
double avg=0.0;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Creating an Object for Random Class Object
Random rad = new Random();
//Getting an Integer Entered by the user
System.out.print("Enter an Integer :");
n=sc.nextInt();
//Create an Array Based on the user entered number
int randNos[]=new int[n];
//Displaying the generated Random Numbers
System.out.println(n+" numbers are generated :");
for(int i=0;i<n;i++)
{
randNos[i]=rad.nextInt(99)+1;
System.out.printf("%5d",randNos[i]);
sum+=randNos[i];
count++;
if(count%10==0)
System.out.println();
}
//calculating Average
avg=(double)sum/n;
//Displaying the Results
System.out.println(" The Sum of the Random Numbers is :"+sum);
System.out.printf(" The Average of the Random Numbers is :%.2f ",avg);
//Counting the numbers greater than Average
for(int i=0;i<n;i++)
{
if(randNos[i]>avg)
{
avgCnt++;
}
}
count=0;
//Displaying the numbers greater than Average
System.out.println(" There are "+avgCnt+" random numbers above the average :");
for(int i=0;i<n;i++)
{
if(randNos[i]>avg)
{
System.out.printf("%5d",randNos[i]);
count++;
}
if(count%10==0)
System.out.println();
}
}
}
_______________________
Output:
Enter an Integer :20
20 numbers are generated :
18 50 25 93 10 34 72 56 3 4
62 57 31 9 40 45 46 35 32 43
The Sum of the Random Numbers is :765
The Average of the Random Numbers is :38.25
There are 10 random numbers above the average :
50 93 72 56 62 57 40 45 46 43
____________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.