Home »
.Net »
C# Programs
Merge two arrays into third array using C# program
Given two arrays, we have to add them into the third array.
[Last updated : March 19, 2023]
C# - Merge Two Arrays
Given two integer arrays and we have to merge them and store in third array.
Example
For example we have two arrays:
Input:
ARR1: 10 20 30 40 50
ARR2: 60 70 80 90 100
Output:
Now we merge above arrays into
another temporary array ARR3:
ARR3: 10 20 30 40 50 60 70 80 90 100
C# program to merge two arrays
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
class Program {
static void Main() {
int i = 0;
int j = 0;
int[] arr1 = new int[5];
int[] arr2 = new int[5];
int[] arr3 = new int[10];
//Read numbers into array
Console.WriteLine("Enter elements of ARR1 : ");
for (i = 0; i < 5; i++) {
Console.Write("Element[" + (i + 1) + "]: ");
arr1[i] = int.Parse(Console.ReadLine());
}
//Read numbers into array
Console.WriteLine("Enter elements of ARR2 : ");
for (i = 0; i < 5; i++) {
Console.Write("Element[" + (i + 1) + "]: ");
arr2[i] = int.Parse(Console.ReadLine());
}
//Merge arr1 and arr2 to arr3
for (i = 0, j = 0; i < 5; i++) {
arr3[j++] = arr1[i];
}
for (i = 0; i < 5; i++) {
arr3[j++] = arr2[i];
}
//Print merged array
Console.WriteLine("Elements of ARR3 : ");
for (i = 0; i < 10; i++) {
Console.WriteLine("Element[" + (i + 1) + "]: " + arr3[i]);
}
Console.WriteLine();
}
}
}
Output
Enter elements of ARR1 :
Element[1]: 10
Element[2]: 20
Element[3]: 30
Element[4]: 40
Element[5]: 50
Enter elements of ARR2 :
Element[1]: 60
Element[2]: 70
Element[3]: 80
Element[4]: 90
Element[5]: 100
Elements of ARR3 :
Element[1]: 10
Element[2]: 20
Element[3]: 30
Element[4]: 40
Element[5]: 50
Element[6]: 60
Element[7]: 70
Element[8]: 80
Element[9]: 90
Element[10]: 100
C# Basic Programs »