Monday 8 February 2016

Java program for insertion of elements using an array



import java.util.Scanner;
class Main{
public static void main(String args[]){
int i,number=8;
int list[]=new int[20];
Scanner input=new Scanner(System.in);
for(i=1;i<=number;i++){
System.out.println("Enter the numbers : ");
list[i]=input.nextInt();
}
System.out.println("Enter the number to be inserted : ");
int num=input.nextInt();
System.out.println("Enter the position");
int pos=input.nextInt();
for(i=number;i>=pos;i--){
list[i+1]=list[i];
}
list[pos]=num;
if(pos>number){
System.out.println("Insertion outside the array");
}
number++;
for(i=1;i<=number;i++){
System.out.println("New array"+list[i]);
}
}
}

OUTPUT:-
C:\Users\saty\Desktop>javac 1.java
C:\Users\saty\Desktop>java Main
Enter the numbers :
12
Enter the numbers :
4
Enter the numbers :
34
Enter the numbers :
54
Enter the numbers :
68
Enter the numbers :
36
Enter the numbers :
42
Enter the numbers :
16
Enter the number to be inserted :
65
Enter the position
5
New array12
New array4
New array34
New array54
New array65
New array68
New array36
New array42
New array16
 

No comments:

Post a Comment