Page 314 - Cs_withBlue_J_C11_Flipbook
P. 314

The merged array ar3 is:

                  Index        0           1           2          3           4           2          3          4
                   Ar          10          5          14          7           66         11          22         33



                                                                                  st
                                                                                         nd
                      Note:  The third array created will have the size of the total size of 1  and 2  arrays.



                Program 16     Write a program in Java to merge two array elements and display the new array.

                 1       import java.util.*;

                 2       class merge
                 3       {

                 4           public static void main()
                 5           {

                 6               Scanner sc= new Scanner(System.in);
                 7               int ar1[ ]=new int[5];

                 8               int ar2[ ]=new int[5];
                 9               int ar3[ ]=new int[10];

                10               int i, j;
                11               for (i=0; i<5; i++)

                12               {
                13                   System.out.print("Enter element in 1st array: ");
                14                   ar1[i] =sc.nextInt();

                15               }

                16               for (i=0; i<5; i++)
                17               {
                18                   System.out.print("Enter element in 2nd array: ");

                19                   ar2[i] =sc.nextInt();
                20               }

                21               //Merging the two arrays
                22               for (i=0; i<5; i++)

                23               {

                24                   ar3[i]=ar1[i];
                25               }
                26               j=5;





                312312  Touchpad Computer Science-XI
   309   310   311   312   313   314   315   316   317   318   319