📊⚡Beginner
Merge Two Arrays
Combine two arrays
Merge two arrays into a single array by copying elements from both.
Program Code
merge_arrays.c
C
1#include <stdio.h>23int main() {4 int arr1[] = {1, 3, 5, 7};5 int arr2[] = {2, 4, 6, 8, 10};6 int n1 = 4, n2 = 5;7 8 int merged[n1 + n2];9 10 // Copy first array11 for (int i = 0; i < n1; i++) {12 merged[i] = arr1[i];13 }14 15 // Copy second array16 for (int i = 0; i < n2; i++) {17 merged[n1 + i] = arr2[i];18 }19 20 printf("Array 1: ");21 for (int i = 0; i < n1; i++) printf("%d ", arr1[i]);22 23 printf("\nArray 2: ");24 for (int i = 0; i < n2; i++) printf("%d ", arr2[i]);25 26 printf("\nMerged: ");27 for (int i = 0; i < n1 + n2; i++) printf("%d ", merged[i]);28 printf("\n");29 30 return 0;31}Output
Array 1: 1 3 5 7
Array 2: 2 4 6 8 10
Merged: 1 3 5 7 2 4 6 8 10
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials