C program to copy all elements of one array to another

Write a C program to read elements in array and copy all elements of first array into second array. C program to copy elements of array.

Required knowledge:

Basic C programming, For loop, Array

Program:

/**
 * C program to copy one array to another array
 */

#include <stdio.h>

int main()
{
    int first[100], second[100];
    int i, size;

    /*
     * Reads size and elements in the array from user
     */
    printf("Enter the size of the array : ");
    scanf("%d", &size);
    printf("Enter elements of first array : ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &first[i]);
    }

    /*
     * Copies all elements from first array to second array
     */
    for(i=0; i<size; i++)
    {
        second[i] = first[i];
    }

    /* 
     * Prints all elements of first array
     */
    printf("\nElements of first array are : ");
    for(i=0; i<size; i++)
    {
        printf("%d\t", first[i]);
    }

    /*
     * Prints all elements of second array
     */
    printf("\nElements of second array are : ");
    for(i=0; i<size; i++)
    {
        printf("%d\t", second[i]);
    }

    return 0;
} 
Output
X
_
Enter the size of the array : 10
Enter elements of first array : 10 20 30 40 50 60 70 80 90 100

Elements of first array are : 10        20        30        40        50        60        70        80        90        100
Elements of second array are : 10        20        30        40        50        60        70        80        90        100

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment