Showing posts with label Array programs. Show all posts
Showing posts with label Array programs. Show all posts

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
 

Sunday, 7 February 2016

Java program for deletion of elements using array




import java.util.Scanner;
class Main{
public static void main(String args[]){
int i,num=8;
int list[]=new int[num];
Scanner input=new Scanner(System.in);
for(i=0;i<list.length;i++){
System.out.println("Enter the array : ");
list[i]=input.nextInt();
}
System.out.println("Enter the location you want to delete : ");
int pos=input.nextInt();
if(pos>=num+1){
System.out.println("Deletion is not possible ");
}
else
for(i=pos-1;i<num-1;i++){
list[i]=list[i+1];
}
for(i=0;i<num-1;i++){
System.out.println("Array is "+list[i]);
}
}
}



OUTPUT:-

C:\Users\saty\Desktop>javac 1.java
C:\Users\saty\Desktop>java Main
Enter the array :

1

Enter the array :

2

Enter the array :

3

Enter the array :

4

Enter the array :

5

Enter the array :

6

Enter the array :

7

Enter the array :

8

Enter the location you want to delete :

4

Array is 1

Array is 2

Array is 3

Array is 5

Array is 6

Array is 7

Array is 8