C Program To Copy The Elements Of An Array From One To Another

Hi there!! the topic is a C program to copy the elements from one array to another array and display it.

The verbal resolution of the given C code is at first

  1. Declare an array and enter elements into it.
  2. Declare another array and enter elements into it
  3. Create a for loop to have iterations to copy the elements for the first array to the second or vice versa
  4. Copy the elements from one array to another using the “=” operator
  5. Use a for loop to iteratively display the elements
  6. Print the elements using printf statement
  7. The source code is given below
#include<stdio.h> //Header file
main() //Main function definition
{
	int n,arr1[20],arr2[20],i;    

   /* variable ‘n’ of integer datatype declared …...array of size 20 declared  as integer arr1[20], same goes for arr2[20] and the integer datatype variable i is declared*/                             	

   printf("\nEnter the number of elements you want to insert\n");
   /*Print statement added giving instruction to the user to print the number of    elements you want to insert*/

   scanf("%d", &n);

   //input taken  n as the input variable with integer format specifier

   printf("\nEnter the elements:\n");

   //instruction given to the user as print statement to enter the elements

   for(i=0;i<n;i++)
   //For loop applied to input the elements to have n iterations possible 
   {
	scanf("%d", &arr1[i]);
	//input taken for array arr1 as integer elements with index i
   }
   for(i=0;i<n;i++) 
   //for loop implemented to input the elements of second array
   {
	arr2[i]=arr1[i]; 
    //PRIME LOGIC: ELEMENTS ARE COPIED FROM ONE ARRAY TO OTHER
 
   }

   printf("\nElements of array 2 are\n"); 
   //print statement to enter the elements of second array 

   for(i=0;i<n;i++)
   //for loop to print the final elements of array 2
   {
	printf("%d", arr2[i]);
    //on print function execution it shall print the array 2 elements
   }
}

What actually happens when you run the code?????

After the adding of the header file and the main function, we add the printf statement to enter the number of elements into our array.

Then we take the input for an integer datatype by using “n” as a variable.

Again we add a printf statement instructing the addition of elements into the array. After that, we add a for loop for multiple iterations to insert the elements into it.

After the addition of for loop, the control keeps on moving into the for loop block and thus it gets iterating until it reaches its limit.

Then a scanf statement is given which is enables the user to input the integer elements into the array using the arr[i] variable.

Again a for loop is initialized ……which is used for the purpose of iterations when elements are copied from one array to the other one by one.

Then the values of the first array are stored in the next declared array and it continues until the loop ends its iteration.

Finally, the contents of the latter array are displayed when  the for loop is initailized and the elements are printed using the printf statement one by one powered by the iterations of the given for loop.

The output is as given :

Enter the number of elements you want to insert

INPUT BY USER: 5

Enter the elements

1

2

3

4

5

Elements of array 2 are:

1

2

3

4

5

If you find any issue on this article please contact us.

Join the what’s app for more information.

Article Written By: Prabhu Darshan

Share Your Love
Default image
Prabhudarshan Samal
Articles: 2

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply