You are on page 1of 5

PROGRAM 8 (ii):

public class arr4


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int i,n,pos;

System.out.println("Enter the number of elements:") ;


n = sc.nextInt();
int[] a = new int[n];

System.out.println("Enter the elements") ;


for(i=0;i<n;i++)
{
a[i] = sc.nextInt();
}

System.out.println("Enter the position of the number which is to be deleted");


pos = sc.nextInt();

for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;

System.out.println("\nOn deleting new array we get is\n");


for(i=0;i<n;i++)
{
System.out.println("a["+i+"] = "+a[i]);
}
}
}
OUTPUT:
PROGRAM 8(i):
import java.util.*;
public class InsertArrayElement
{
public static void main(String[] args) {
int count, i, num, index;
int input[] = new int[10];

Scanner scanner = new Scanner(System.in);


System.out.println("Enter Number of Elements in Array");
count = scanner.nextInt();

/*
* Take array input from user
*/
System.out.println("Enter " + count + " Numbers");
for (i = 0; i < count; i++) {
input[i] = scanner.nextInt();
}

System.out.println("Enter Number to be Inserted");


num = scanner.nextInt();
System.out.println("Enter Index of Insertion");
index = scanner.nextInt();
/*
* Insert "num" at index. First shift all element right of index by one
* position
*/
for (i = count; i > index; i--) {
input[i] = input[i - 1];
}
OUTPUT:

You might also like