Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Tuesday 6 February 2018

C program to merge two Arrays

#include<stdio.h> void main() { int arr1[10], arr2[10], res[20]; int i, j, k, n1, n2; printf("\nEnter no of elements in 1st array :"); scanf("%d", &n1); for (i = 0; i < n1; i++) { scanf("%d", &arr1[i]); } printf("\nEnter no of elements in 2nd array :"); scanf("%d", &n2); for (i = 0; i < n2; i++) { scanf("%d", &arr2[i]); } i = 0; j = 0; k = 0; while (i < n1 && j < n2) { if (arr1[i] <= arr2[j]) { res[k] = arr1[i]; i++; k++; } else { res[k] = arr2[j]; k++; j++; } } while (i < n1) { res[k] = arr1[i]; i++; k++; } while (j < n2) { res[k] = arr2[j]; k++; j++; } printf("Merged array is :\n"); for (i = 0; i < n1 + n2; i++) printf("%d ", res[i]); }

No comments:

Post a Comment