Merge two arrays in c: It is a simple and easy program using an array to add two different arrays in one single array

To merge the two arrays we use simple Technique
We use one third array to merge two arrays in c.

Example:
array1 = 1, 2, 3, 4,5
array2 = 6,7,8
Merge two arrays
array3 = 1, 2, 3, 4, 5, 6, 7, 8,

Algorithm to merge two array in c

  1. Declare variables. a1[], a2[], merge[], j=0
  2. Take first array input from user. a1[]
  3. Take second array input from user. a2[]
  4. Traverse first array
    For i = 0 to end
    Copy array
    merge[j++] = a[i++]
  5. Traverse first array
    For i = 0 to end
    Copy array
    merge[j++] = a[i++]
  6. Print merged array

C program to merge two arrays.

#include<stdio.h>
main()
{
int i,size1,size2,j=0,temp;
printf("Enter size of an array1\n");
scanf("%d",&size1);
printf("Enter size of an array2\n");
scanf("%d",&size2);
int a[size1],b[size2],c[size1+size2];
printf("Enter numbers for array 1\n");
for(i=0;i<size1;i++)
{
scanf("%d",&a[i]);

}

printf("Enter numbers for array 2\n");
for(i=0;i<size2;i++)
{
scanf("%d",&b[i]);
}
//merging
for(i=0;i<size1;i++)
{
c[j]=a[i];
j++;
}
for(i=0;i<size2;i++)
{
c[j]=b[i];
j++;
}
printf("Array after merging\n");
for(i=0;i<size1+size2;i++)
{
printf("%d\n",c[i]);
}
}

Output :

Merge two arrays in c

Write A Comment