In: Computer Science
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array.
2 .Find the output of the following Java program and explain your answer
import java.util.Scanner;
public class RandomNumbers {
public static void main(String... args){
Scanner scan = new
Scanner(System.in);
System.out.print("Enter any number: ");
int n=scan.nextInt();
int[] arr;
arr= new int[n];
int min=5,max=555;
int sum=0;
double average;
for(int i=0;i<n;i++){
arr[i]=(int)(Math.random()*((max-min)+1))+min;
sum+=arr[i];
}
average = (double)sum/n*1.0;
System.out.println("Sum: " +
sum);
System.out.println("Average: " +
average);
}
}
OUTPUT:
As you can see the average comes closer and closer to 280 which is (5+555)/2.