Write a program in to array size to be user input print it


This Java program prompts the user to input the size of an array, takes input values from the user, and then prints out the values of the array. Here's a brief explanation of what the program does:

  • The program first creates a new Scanner object to read user input from the console.
  • The user is prompted to enter the size of the array.
  • An array of integers 'a' is created with size equal to the value entered by the user.
  • A for loop is used to iterate over each element of the array, prompting the user to enter the value of each element.
  • Another for loop is used to print out the elements of the array.
  • The values of the array are printed to the console.

Source Code

import java.util.Scanner;
class Array_Size
{
	public static void main(String[] args)
	{   
		Scanner input =new Scanner(System.in);
		System.out.print("Enter the Array Limit :");
		int l =input.nextInt();
		int [] a =new int[l];
		for(int i=0;i<l;i++)
		{
			System.out.printf("Element of a[%d] :",i);
			a[i]=input.nextInt();
		} 
		System.out.println("\nDisplay Array Elements...\n");
		for(int e:a)
		{
			System.out.println(e);
		}
    }
}
 

Output

Enter the Array Limit :4
Element of a[0] :10
Element of a[1] :20
Element of a[2] :30
Element of a[3] :40

Display Array Elements...

10
20
30
40

Example Programs