codescracker


c

C Program to Merge Two Arrays



« Previous Program Next Program »


Merge Two Arrays in C

To merge two arrays in C programming, start adding the element of the first array to the third array (target array) after this start appending the elements of the second array to the third array (target array)

C Programming Code to Merge Two Arrays

Following C program ask to the user to enter array 1 and 2 size, then ask to enter array 1 and 2 elements, to merge or add to form new array, then display the result of the added array or merged array (Here we display the direct merged array). You can also sort the two array then merge or sort after merge. So to learn about sorting, here are the techniques to sort arrays:

/* C Program - Merge Two Arrays */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr1[50], arr2[50], size1, size2, size, i, j, k, merge[100];
	printf("Enter Array 1 Size : ");
	scanf("%d",&size1);
	printf("Enter Array 1 Elements : ");
	for(i=0; i<size1; i++)
	{
		scanf("%d",&arr1[i]);
	}
	printf("Enter Array 2 Size : ");
	scanf("%d",&size2);
	printf("Enter Array 2 Elements : ");
	for(i=0; i<size2; i++)
	{
		scanf("%d",&arr2[i]);
	}
	for(i=0; i<size1; i++)
	{
		merge[i]=arr1[i];
	}
	size=size1+size2;
	for(i=0, k=size1; k<size && i<size2; i++, k++)
	{
		merge[k]=arr2[i];
	}
	printf("Now the new array after merging is :\n");
	for(i=0; i<size; i++)
	{
		printf("%d  ",merge[i]);
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program merge two array

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test