Friday, June 8, 2012

Write a program to insert element in existing array.?


♣♣♣ WRITE A PROGRAM TO INSERT ELEMENT IN EXISTING ARRAY. ♣♣♣

import java.io.*;
class pro6
{
            public static void main(String args[]) throws IOException
            {
            int cnt,last,pos,num;
            int arr[]=new int[10];
            int newarr[]=new int[10];
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the total number of array : ");
            last=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                        System.out.print("Enter element"+(cnt+1)+" : ");
                        arr[cnt]=Integer.parseInt(b.readLine());        
                        }
                     System.out.print("Enter the position to insert new element in an existing array :");
                        pos=Integer.parseInt(b.readLine());
                        System.out.print("Enter the element : ");
                        num=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                                    if(cnt+1==pos)
                                    newarr[cnt]=num;
                                    else
                                    newarr[cnt]=arr[cnt];
                        }
                        System.out.println("Array after inserting new element is :");
                        for(cnt=0;cnt
                        {
                        System.out.print(newarr[cnt]+"   ");  
                        }         
            }
}
OUTPUT :-

            Enter the total number of array : 5
Enter Element1 :23
Enter Element2 :12
Enter Element3 :45
Enter Element4 :33
Enter Element5 :21
Enter the position to insert new element in an existing array :2
Enter the element : 88
Array after inserting new element is :
            23        88        45        33        21

No comments:

Post a Comment