/* Java Program Example - Insert Element in Array */
import java.util.Scanner;
public class JavaProgramArrayInsert
{
public static void main(String args[])
{
int size, insert, i, pos,count=0;
int arr[] = new int[50];
char choice=' ';
Scanner scan = new Scanner(System.in);
System.out.print("Enter Array Size : ");
size = scan.nextInt();
System.out.print("Enter Array Elements : ");
for(i=0; i<size; i++)
{
arr[i] = scan.nextInt();
}
System.out.print("Enter Element to be Insert : ");
insert = scan.nextInt();
System.out.print("At Which Position ? (Enter Index Number ) : ");
pos = scan.nextInt();
for(i=size; i>pos; i--)
{
arr[i] = arr[i-1];
}
arr[pos] = insert;
System.out.print("Element inserted Successfully..!!\n");
System.out.print("Now the New Array is :\n");
for(i=0; i<size+1; i++)
{
System.out.print(arr[i]+ " ");
}
}
}
Output:
import java.util.Scanner;
public class JavaProgramArrayInsert
{
public static void main(String args[])
{
int size, insert, i, pos,count=0;
int arr[] = new int[50];
char choice=' ';
Scanner scan = new Scanner(System.in);
System.out.print("Enter Array Size : ");
size = scan.nextInt();
System.out.print("Enter Array Elements : ");
for(i=0; i<size; i++)
{
arr[i] = scan.nextInt();
}
System.out.print("Enter Element to be Insert : ");
insert = scan.nextInt();
System.out.print("At Which Position ? (Enter Index Number ) : ");
pos = scan.nextInt();
for(i=size; i>pos; i--)
{
arr[i] = arr[i-1];
}
arr[pos] = insert;
System.out.print("Element inserted Successfully..!!\n");
System.out.print("Now the New Array is :\n");
for(i=0; i<size+1; i++)
{
System.out.print(arr[i]+ " ");
}
}
}
Output:
No comments:
Post a Comment