C Program To Merge Two Arrays

C program to merge two arrays; Through this tutorial, we will learn how to merge two arrays using standard method and function in c programs.

Programs To Merge Two Arrays in C

To merge two arrays using standard method and function in c programs:

  • C Program To Merge Two Arrays using Standard Method
  • C Program To Merge Two Arrays using Function

C Program To Merge Two Arrays using Standard Method

#include <stdio.h>

print(int *a,int n)
 {
    int i;



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

        	printf("%d  ",a[i]);


    }

 }


int main()
{
    int a[10000],b[10000],c[20000],i,n1,n2 ;

    printf("Enter size of the 1st array : ");
    scanf("%d", &n1);
    printf("Enter elements in array : ");
    for(i=0; i<n1; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter size of the 2nd array : ");
    scanf("%d",&n2);

    printf("Enter elements in array : ");
    for(i=0; i<n2; i++)
    {
        scanf("%d",&b[i]);
    }

    for(i=0; i<n1+n2; i++)
    {
       if(i<n1)
          c[i]=a[i];
          else
            c[i]=b[i-n1];

    }

    printf(" 1st array :\n");

    print(a,n1);

	printf("\n2nd array :\n");

    print(b,n2);

	printf("\nMerged array :\n");

    print(c,n1+n2);

    return 0;
}

The output of the above c program; as follows:

Enter size of the 1st array : 5
Enter elements in array : 1 2 3 4 5
Enter size of the 2nd array : 3
Enter elements in array : 6 7 8
1st array :
1  2  3  4  5
2nd array :
6  7  8
Merged array :
1  2  3  4  5  6  7  8  

C Program To Merge Two Arrays using Function

#include <stdio.h>

merge(int *a,int *b,int *c,int n1,int n2)
{ int i;

    for(i=0; i<n1+n2; i++)
    {
         if(i<n1)
          c[i]=a[i];
          else
            c[i]=b[i-n1];
	}





 }

print(int *a,int n)
 {
    int i;



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

        	printf("%d  ",a[i]);


    }

 }

int main()
{
    int a[10000],b[10000],c[20000],i,n1,n2,n ;

    printf("Enter size of the 1st array : ");
    scanf("%d", &n1);
    printf("Enter elements in array : ");
    for(i=0; i<n1; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter size of the 2nd array : ");
    scanf("%d",&n2);

    printf("Enter elements in array : ");
    for(i=0; i<n2; i++)
    {
        scanf("%d",&b[i]);
    }

    merge(a,b,c,n1,n2);

    printf(" 1st array :\n");

    print(a,n1);

	printf("\n2nd array :\n");

    print(b,n2);

	printf("\n3rd array :\n");

    print(c,n1+n2);
} 

The output of the above c program; as follows:

Enter size of the 1st array : 5
Enter elements in array : 1 2 3 4 5
Enter size of the 2nd array : 3
Enter elements in array : 6 7 8
1st array :
1  2  3  4  5
2nd array :
6  7  8
Merged array :
1  2  3  4  5  6  7  8  

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *