Java Program to take input and print elements of array

Java Program to take input and print elements of array

In this tutorial, we are going to see the very first and basic program of the array in Java. Here we will learn to take the input of array elements from the user. As well as we will see how you can iterate the array element using for loop.

import java.util.*;
class Main{
    public static void main(String ...a){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the size of array: ");
      int n = sc.nextInt();
      int arr[] = new int[n];
      System.out.println("Enter " +(n)+ " array elements: ");
      for(int i=0; i<n; i++) {
         arr[i] = sc.nextInt();
      }
      System.out.println("Elements in array are: ");
      for(int i=0; i<n; i++) {
         System.out.println(arr[i]);
      }
    }
}

Output

Java take input and print array element