C Programming Tutorial

Advertisements

Write a program to merge two arrays of different size in C



       #include<stdio.h>
       #include<conio.h>
       void main()
       {
              int arr1[6],arr2[4],arr3[10],i,j,num;
              clrscr();

              printf("\nEnter values for array 1");
              for(i=0;i<6;i++)
              {
                 printf("\nEnter any number : "); 
                 scanf("%d",&arr1[i]); 
              }
              
              printf("\n\nEnter values for array 2");
              for(i=0;i<4;i++)
              {
                 printf("\nEnter any number : "); 
                 scanf("%d",&arr2[i]); 
              }
              
              printf("\nValues in array 1 : ");
              for(i=0;i<6;i++)
              printf("  %d",arr1[i]);
              
              printf("\nValues in array 2 : ");
              for(i=0;i<4;i++)
              printf("  %d",arr2[i]);              
              
              for(i=0;i<10;i++)	
              {
		              if(i<6)
		                     arr3[i] = arr1[i];
		              else
		                     arr3[i] = arr2[i-6];
              }

              printf("\nValues in array 3 : ");
              for(i=0;i<10;i++)
              printf("  %d",arr3[i]);

              getch();
}

   Output : 

              Enter values for array 1 :
              Enter any number : 78
              Enter any number : 45
              Enter any number : 12
              Enter any number : 89
              Enter any number : 56
              Enter any number : 23
              
              Enter values for array 2 :              
              Enter any number : 79
              Enter any number : 46
              Enter any number : 13
              Enter any number : 82
              
              Values in array 1 : 78 45 12 89 56 23
              Values in array 2 : 79 46 13 82
              Values in array 3 : 78 45 12 89 56 23 79 46 13 82




Advertisement