C C++ program to merge two arrays

Coding in C

#include<stdio.h>
int main()
{
    int a1[5],a2[5],a3[10],i=0,j=0;
    printf("Enter elements of Array1:\n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&a1[i]);
    }
    printf("Enter elements of Array1:\n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&a2[i]);
    }
    for(i=0;i<10;i++)
    {
        if(i<5)
        {
         a3[i]=a1[i];
        }
        else
        {
            a3[i]=a2[j];
            j++;
        }
    }
    printf("Merged Array:\n");
    for(i=0;i<10;i++)
    {
        printf("%d ",a3[i]);
    }
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int a1[5],a2[5],a3[10],i=0,j=0;
    cout<<"Enter elements of Array1:\n";
    for(i=0;i<5;i++)
    {
        cin>>a1[i];
    }
    cout<<"Enter elements of Array1:\n";
    for(i=0;i<5;i++)
    {
        cin>>a2[i];
    }
    for(i=0;i<10;i++)
    {
        if(i<5)
        {
         a3[i]=a1[i];
        }
        else
        {
            a3[i]=a2[j];
            j++;
        }
    }
    cout<<"Merged Array:\n";
    for(i=0;i<10;i++)
    {
        cout<<a3[i]<<" ";
    }
    return 0;
}

Output

C C++ program to merge two arrays

Published by

admin

I am a computer teacher, programmer and web developer