Answer to Question #272993 in Java | JSP | JSF for harlington

Question #272993

Instructions

  1. Create a variable that accepts a positive integer.
  2. Create an empty array. Then, using loops, add random integer values into the array one by one. The number of values to be stored is dependent on the inputted positive integer given in the first instruction.
  3. Print out the largest integer among all the elements in the array, by any means necessary.


Input

The first line contains the size of the array/list.

The next lines contains an integer on each.

5
5
34
3
23
10

Output

A line containing an integer.

34
1
Expert's answer
2021-11-29T18:45:16-0500


package random;


import java.util.Random;
import java.util.Scanner;
public class RandomArray {


    
    public static void main(String[] args) {
       
        Random rand = new Random();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the size of the array\n");
        int n = scan.nextInt();
        int [] arr = new int[n];
        for(int i=0; i<n; i++){
           int x=  rand.nextInt(50);
            arr[i] = x;
        }
        int max = arr[0];
       for(int i=0; i<n; i++){
           if(arr[i]>max){
               max = arr[i];
           }
       }
        System.out.println("The largest is: "+max);
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS