Answer:
// code in java.
// package
import java.util.*;
// class definition
class Main
{
// method to print array elements
public static void PrintArray(int []inventory)
{
// find size of array
int len=inventory.length;
System.out.println("Elements of the array are:");
// print all the elements
for(int x=0;x<len;x++)
{
System.out.print(inventory[x]+" ");
}
}
// mai method
public static void main (String[] args) throws java.lang.Exception
{
try{
// scanner object to read input
Scanner scr=new Scanner(System.in);
System.out.print("Enter the number of elements:");
// read the size of array
int n=scr.nextInt();
// create an array of size n
int inventory[]=new int[n];
System.out.print("Enter the elements of array:");
// read the elements of array
for(int a=0;a<n;a++)
{
inventory[a]=scr.nextInt();
}
// call the method
PrintArray(inventory);
}catch(Exception ex){
return;}
}
}
Step-by-step explanation:
Read the number of elements and assign to "n".Then read the "n" elements and store them into array inventory.Call the method PrintArray()with argument inventory.This method will print all the elements of the array inventory.
Output:
Enter the number of elements:5
Enter the elements of array:3 2 9 6 5
Elements of the array are:
3 2 9 6 5