w3resource

C Exercises: Copy the elements of one array into another array

C Array: Exercise-4 with Solution

Write a program in C to copy the elements of one array into another array.

Pictorial Presentation:

C Exercises: Copy the elements of one array into another array

Sample Solution:

C Code:

#include <stdio.h>

void main()
{
    int arr1[100], arr2[100];
    int i, n;
	
	
       printf("\n\nCopy the elements one array into another array :\n");
       printf("----------------------------------------------------\n");
	   
       printf("Input the number of elements to be stored in the array :");
       scanf("%d",&n);
   
       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
        {
	      printf("element - %d : ",i);
	      scanf("%d",&arr1[i]);
	    }
    /* Copy elements of first array into second array.*/ 
    for(i=0; i<n; i++)
    {
        arr2[i] = arr1[i];
    }

    /* Prints the elements of first array   */
    printf("\nThe elements stored in the first array are :\n");
    for(i=0; i<n; i++)
    {
        printf("% 5d", arr1[i]);
    }

    /* Prints the elements copied into the second array. */
    printf("\n\nThe elements copied into the second array are :\n");
    for(i=0; i<n; i++)
    {
        printf("% 5d", arr2[i]);
    }
	       printf("\n\n");
}

Sample Output:

Copy the elements one array into another array :                                                              
----------------------------------------------------                                                          
Input the number of elements to be stored in the array :3                                                     
Input 3 elements in the array :                                                                               
element - 0 : 15                                                                                              
element - 1 : 10                                                                                              
element - 2 : 12                                                                                              
                                                                                                              
The elements stored in the first array are :                                                                  
   15   10   12                                                                                               
                                                                                                              
The elements copied into the second array are :                                                               
   15   10   12 

Flowchart:

Flowchart: Copy the elements one array into another array.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to find the sum of all elements of an array.
Next: Write a program in C to count a total number of duplicate elements in an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP