C Program to Concatenate Two Arrays

In this c programming example we will learn how to concatenate two arrays

Algorithm

  1. Take an two array of integers by scanf() function
  2. Assign values to two different variables a and b
  3. Take other variable c with size as a & b
  4. Iterate first array and add items into array c from array a
  5. Similarly iterate array b and add items into array c
  6. Now array c contains combination of array a & b
  7. Print the sum of array at the end of the loop

 

Simple C example to concatenate two arrays

#include <stdio.h>

int main() {
    
   int c[10];
   
   int a[5] = {1,3,5,6,8};
   
   int b[5]  = {99,78,23,34,12};
   
   int loop, index, len;
   
   len= 5;
   
   index = 0;
 
   for(loop = 0; loop < len; loop++) {
       
      c[index] = a[loop];
      
      index++;
      
   }
 
   for(loop = 0; loop < len; loop++) {
       
      c[index] = b[loop];
      
      index++;
      
   }

   printf("\nEven -> ");
   
   for(loop = 0; loop < len; loop++)
      printf(" %d", a[loop]);
      
   printf("\nOdd  -> ");
   
   for(loop = 0; loop < len; loop++)
      printf(" %d", b[loop]);

   printf("\nConcat -> ");
   
   for(loop = 0; loop < 10; loop++)
      printf(" %d", c[loop]);
   
   return 0;
}

 

Output

Even ->  1 3 5 6 8

Odd  ->  99 78 23 34 12  

Concat ->  1 3 5 6 8 99 78 23 34 12