Write a c program to merge two array in single array

07 Sep 2022 Balmiki Mandal 0 C Programming

C Program to merge two arrays in a single array in c

Merging two arrays into a single array involves taking all the elements from both arrays and creating a new array that contains all the elements in a single sequence. 

To merge two arrays into a single array in C, we can use the following steps:

  1. Declare a third array to store the merged array. The size of the third array should be equal to the sum of the sizes of the two original arrays.
  2. Copy the elements of the first original array to the merged array.
  3. Copy the elements of the second original array to the merged array.

C program shows how to merge two arrays into a single array:

#include
int main()
{
    int a[5]={10,20,30,40,50};
    int b[5]={60,70,80,90,100};
    int c[10],i;
    printf("Murged array is\n");
    for(i=0;i<5;i++)
    {
        c[i]=a[i];
        c[i+5]=b[i];
    }
    for(i=0;i<10;i++)
    printf(" %d",c[i]);
    printf("\n");
}

Output:

The merged array is: 1 2 3 4 5 6 7 8 9 10

Top Resources

Write a program to merge two strings alternatively in c

Write a c program to merge two arrays in single array 

write a program to merge two string in a single single string.

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.