Mar 14, 2018

Java Program to Insert Elements in an Array

Written by

Insert Element in Array: To Insert element in array we have to take the size of the array and element from user to make it easy

Before Going to Source Code Let Us Make You Understand:

1.First Declare the Array and Take user input as Size.
2.Take the element from user.
3.Run for loop up to size of the array.
4.Insert element in an array in between for loop.

To Print Array’s Values:
1.Run for loop up to size of the array.
2.Print array.

Source Code:

import java.util.*;
public class yes {
public static void main(String args[]){

// Taking array size and elements form user.

int a[],i; // Declaring array namer
System.out.println("Please Enter The array Size");
Scanner s = new Scanner(; // Scanner for Array Size.
int size = s.nextInt(); // Scanned Value for Array Size from Use Input.
a = new int[size]; // Scanner Valued Insert in Array Size.
System.out.println("Enter the Number");
Scanner sh = new Scanner(; // Taking value to be insert in an Array
for( i = 0; i< size;i++) // Running loop until the array Size.
a[i] = sh.nextInt(); // Inserting scanned valued in array

// Printing array value
System.out.println("The Inserted Array Are");
for(i =0;i<size;i++) // Running loop until the array size reach
System.out.println(a[i]+ "");

Output of Program:

Article Tags:
Article Categories:

Leave a Comment

Your email address will not be published. Required fields are marked *