C program to copy all elements of an array into another array

C program to copy all elements of an array into another array:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

main()
{
   int arr1[30],arr2[30],i,num;

   printf("Enter no of elements :");
   scanf("%d", &num);

   //Accepting values in Array
   printf("\nEnter %d values :",num);
   for(i=0;i<num;i++)
    {
      scanf("%d",&arr1[i]);
    }

   // Copying data from array arr1 to arr2
   for(i=0;i<num;i++)
    {
      arr2[i] = arr1[i];
    }

   //Printing all elements of array
   printf("\nCopied array is :");
   for(i=0;i<num;i++)
    printf("\narr2[%d]= %d",i,arr2[i]);
}

OUTPUT:

Enter no of elements :5

Enter 5 values :7 8 6 4 2
Copied array is :
arr2[0]= 7
arr2[1]= 8
arr2[2]= 6
arr2[3]= 4
arr2[4]= 2

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.