This C Program merge the elements of 2 sorted array.

Here is source code of the C Program to merge the elements of 2 sorted array . The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to Merge the Elements of 2 Sorted Array`

`*/`

`#include <stdio.h>`

void main()

`{`

int array1[50], array2[50], array3[100], m, n, i, j, k = 0;

printf("\n Enter size of array Array 1: ");

scanf("%d", &m);

printf("\n Enter sorted elements of array 1: \n");

for (i = 0; i < m; i++)

`{`

scanf("%d", &array1[i]);

`}`

printf("\n Enter size of array 2: ");

scanf("%d", &n);

printf("\n Enter sorted elements of array 2: \n");

for (i = 0; i < n; i++)

`{`

scanf("%d", &array2[i]);

`}`

i = 0;

j = 0;

while (i < m && j < n)

`{`

if (array1[i] < array2[j])

`{`

array3[k] = array1[i];

`i++;`

`}`

`else`

`{`

array3[k] = array2[j];

`j++;`

`}`

`k++;`

`}`

if (i >= m)

`{`

while (j < n)

`{`

array3[k] = array2[j];

`j++;`

`k++;`

`}`

`}`

if (j >= n)

`{`

while (i < m)

`{`

array3[k] = array1[i];

`i++;`

`k++;`

`}`

`}`

printf("\n After merging: \n");

for (i = 0; i < m + n; i++)

`{`

printf("\n%d", array3[i]);

`}`

`}`

$ cc pgm81.c $ a.out Enter size of array Array 1: 4 Enter sorted elements of array 1: 12 18 40 60 Enter size of array 2: 4 Enter sorted elements of array 2: 47 56 89 90 After merging: 12 18 40 47 56 60 89 90

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.