- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Environmental Science
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- articles and Answers
- Effective Resume Writing
- HR Interview articles
- Computer Glossary
- Who is Who
Write C Program to Merge Two arrays in C Programming
Program : C Program to Merge Two arrays in C Programming
#include<stdio.h>
int main() {
int arr1[30], arr2[30], res[60];
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;
// Merging starts
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
res[k] = arr1[i];
i++;
k++;
} else {
res[k] = arr2[j];
k++;
j++;
}
}
/* Some elements in array 'arr1' are still remaining
where as the array 'arr2' is exhausted */
while (i < n1) {
res[k] = arr1[i];
i++;
k++;
}
/* Some elements in array 'arr2' are still remaining
where as the array 'arr1' is exhausted */
while (j < n2) {
res[k] = arr2[j];
k++;
j++;
}
//Displaying elements of array 'res'
printf("\nMerged array is :");
for (i = 0; i < n1 + n2; i++)
printf("%d ", res[i]);
return (0);
}
Output :
Enter no of elements in 1st array : 4
11 22 33 44
Enter no of elements in 2nd array : 3
10 40 80
Merged array is : 10 11 22 33 40 44 80
- Related Questions & Answers
- C Program To Print Your Own Name
- How to Calculate Area of Equilatral Triangle using C Language
- Recursive program to linearly search an element in a given array using C
- How to reads customer number and power consumed and prints amount to be paid using C
- Write C Program to Find Factorial of Number Using Recursion
- How to Check Whether Number is Perfect Or Not using C
- How to find gretest in 3 number using C Programming
- How to calculate sum of 5 subjects and find percentage Using C Program
- Write C Program to Calculate Addition of All Elements in Array
- Write C Program to Find Smallest Element in Array in C Programming
- Write C Program to Delete duplicate elements from an array
- How to Find Area of Scalene Triangle using C
- How to Calculate Area of Right angle Triangle using C Language
- Write C program to Check Whether a Number is Positive or Negative
- Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers
- Write C Program to Find Largest Element in Array in C Programming
Advertisements
ads