Write a program to store elements in an array and print it


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

  • The program creates a new Scanner object to read user input from the console.
  • An array of integers 'a' is created with size 7.
  • 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 Store_Elements
{
	public static void main(String[] args)
	{   
		Scanner input =new Scanner(System.in);
		int [] a =new int[7]; 
		for(int i=0;i<7;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

Element of a[0] :5
Element of a[1] :11
Element of a[2] :22
Element of a[3] :33
Element of a[4] :44
Element of a[5] :55
Element of a[6] :66

Display Array Elements...

5
11
22
33
44
55
66

Example Programs