C Program to Copy the Elements of one Array into Another Array

Ex: Write a C program to copy the elements of one array into another array. How to write a C program to copy the elements of one array into another array. C program to copy the elements of one array into another array.


Input from user:

Enter the limit: 5

Enter elements: 
1
3
5
7
9

Expected output:

Elements in 1st array are: 
arr[0]=1
arr[1]=3
arr[2]=5
arr[3]=7
arr[4]=9

Copied elements in 2nd array are:
arr[0]=1
arr[1]=3
arr[2]=5
arr[3]=7
arr[4]=9




  Step by step logic of the given program:


1. Accept input (limit) from user declare varible say no and arr1.

2. Declare another array arr2 to store copy of arr1.

3. Run a loop from 0 to no and accept elements and store it in arr1 using:
for(i=0;i<no;i++)
{
scanf("%d",&arr1[i]);
}

4. Run another loop and inside loop assign current array elements arr1 to arr2 using:
arr2[i]=arr1[i];

5. In last print the copied elements.





  C program to copy the elements of one array into another array :


#include<stdio.h>
#define max 100
void main()
{
int arr1[max],arr2[max],i,no;

printf("How many elements you are going to enter:\n");
scanf("%d",&no);

printf("Enter elements:\n");
for(i=0;i<no;i++)
{
scanf("%d",&arr1[i]);
}

for(i=0;i<no;i++)
{
arr2[i]=arr1[i];
}

printf("Elements in 1st array are:\n");
for(i=0;i<no;i++)
{
printf("arr[%d]=%d\n",i,arr1[i]);
}

printf("Copied elements in 2nd array are:\n");
for(i=0;i<no;i++)
{
printf("arr[%d]=%d\n",i,arr2[i]);
}
}


Above program shows the following output:

C program to copy the elements of one array into another array , Copy arrays using C


No comments:

Post a Comment

If you have any doubts, please discuss here...👇