C Program to Copy all elements of an array into Another array

In this program, you will take 5 numbers in an array from the user and copy them into another array in the same order.

Copy all Array element into another Array

#include<stdio.h>
int main()
{
   int a1[5],a2[5],i;
   printf("\n enter five element in array:");
   for(i=0;i<5;i++)
	scanf("%d",&a1[i]);
   printf(" source array element are ");
   for(i=0;i<5;i++)
	printf("%d\t",a1[i]);
   printf("\n copy array element are  ");
   for(i=0;i<5;i++)
	a2[i]=a1[i];
   for(i=0;i<5;i++)
	printf("%d\t",a2[i]);
   return 0;
}

output

 enter five element in array:1 3 5 3 2
 source array element are 1     3       5       3       2
 copy array element are  1      3       5       3       2

Logic for Copy all Array element into another Array

  • Initialize two array of size 5. two arrays are a1[5], a2[5]
  • take 5 numbers from user and stored in array a1[5]
  • Copy all element of array a1[5] into array a2[5] using this code for(i=0;i<5;i++){ a2[i]=a1[i]; }