C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Copy array to another

C Program: Copy array to another

C

#include <stdio.h>

 

int main() {

    int arr1[100], arr2[100];

    int n, i;

 

    // Input size of array

    printf("Enter number of elements in the array: ");

    scanf("%d", &n);

 

    // Input elements of first array

    printf("Enter %d elements:\n", n);

    for (i = 0; i < n; i++) {

        scanf("%d", &arr1[i]);

    }

 

    // Copy elements from arr1 to arr2

    for (i = 0; i < n; i++) {

        arr2[i] = arr1[i];

    }

 

    // Display copied array

    printf("\nElements of first array: ");

    for (i = 0; i < n; i++) {

        printf("%d ", arr1[i]);

    }

 

    printf("\nElements of copied array: ");

    for (i = 0; i < n; i++) {

        printf("%d ", arr2[i]);

    }

 

    printf("\n");

    return 0;

}

Output

 
INPUT :
Enter number of elements in the array: 5
Enter 5 elements:
10 20 30 40 50

OUTPUT :
Elements of first array: 10 20 30 40 50
Elements of copied array: 10 20 30 40 50

Explanation

  1. The program takes input for array size and its elements.
  2. It then copies each element of arr1 to arr2 using a simple for
  3. Both arrays are displayed to confirm the copy operation.