Write a Java Program to input and print n elements in an array

By | 25.01.2017

Write a Java  Program to input and print n elements in an array 


  • Write a Java program to read elements in an array and print array. How to input and display elements in an array using for loop in java programming.

Array uses an index based mechanism for fast and easy accessing of elements. Array index starts from 0 to N – 1 (where N is the total number of elements in the array).
Example:
Input size: 5
Input elements: 1
2
3
4
5

Output: 1, 2, 3, 4, 5

Let’s demonstrate this through a program……


SOURCE CODE ::

 

import java.util.Scanner;

public class Arrays {
    
 private static Scanner scanner = new Scanner(System.in);
 
    public static void main(String[] args) {
        
        System.out.println("Enter The Size Of Array U Want : \r");
        int n =scanner.nextInt();
        int[] myintegers = getintegers(n);
        int[] print = printarray(myintegers);        
    }
    
    public static int[] getintegers(int n)
    {
        int [] array = new int[n];
        System.out.println("Enter " + n + " values to the array : \r" );
        for(int i=0;i<array.length;i++)
        {
            array[i]=scanner.nextInt();
        }
        return array;
    }
    
    public static int[] printarray(int[] array)
    {
        System.out.println("Entered Arrays values : \r");
        for(int i=0;i<array.length;i++)
        {
            System.out.println(array[i]);
            
        }
  return array;
    }
}

 

OUTPUT ::

 

Enter The Size Of Array U Want : 
5
Enter 5 values to the array : 
6
2
44
1
88
Entered Arrays values : 
6
2
44
1
88

 

3 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments