Q:

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

0
  • 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……

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now