Write a program in C to copying the array into another array. In this blog, we are going to learn how to create a program to copy one array into another array
Sample input and output
original array : arr=[10,52,78,96,63]
copied array : arr2=[10,52,78,96,63]
Algorithm
- Include library and declare the main method with void as return type
- Initialize required variables
- Using loops for operating logic
- Print output with a suitable message
C program to copying the array into another array
#include<stdio.h> void main(){ int i; int arr[5]={10,52,78,96,63}; int arr2[5]; printf("original array is\n"); for(i=0;i<5;i++){ printf("%d ",arr[i]); } for(i=0;i<5;i++){ arr2[i]=arr[i]; } printf("\ncopied array is\n"); for(i=0;i<5;i++){ printf("%d ",arr2[i]); } }
Explanation of the code
On-Line no:1 include library
On-Line no:2 declare the main method with void as its return type
On-Line no:3 initialize the array and variable
On-Line no:4 initialize another array in which we are going to copy the original array
On-Line no:5 command to print original array
On-Line no:6 using for loop to print the array
On-Line no:7 printing the element
On-Line no:8 closing the scope of the for loop we use for printing the array
On-Line no:9 again using the for loop to copy the array
On-Line no:10 copying the array for original to the new one
On-Line no:11 scope close of for loop
On-Line no:12 printing the message
On-Line no:13 one more time for loop to print the new array
On-Line no:14 print the copied element
On-Line no:15 closing the scope of for loop
On-Line no:16 closing the scope of the main method
Output
In this blog, we have covered a part of the array in which we copy an array to another array