Program to copy all elements of one array into another array
In this program, we need to copy all the elements of one array into another. This can be accomplished by looping through the first array and store the elements of the first array into the second array at the corresponding position.
Array 1
1 2 3 4 5
Array 2
1 2 3 4 5
Algorithm
- 1.START
- 2.INITIALIZE arr1[] ={1, 2, 3, 4, 5}
- 3.CREATE arr2[] of size arr1[].
- 4.COPY elements of arr1[] to arr2[]
- 5. arr2[i] =arr1[i]
- 6.DISPLAY elements of arr1[].
Program
class Q1_copy {
public static void main(String[] args)
//declare array
int arr2[]=new int [5];
int arr[]={11,2,33,4,5};
System.out.println("original array is");
for(int i=0;i< arr.length;i++)
{
System.out.print(arr[i] + " ");
}
//copy array
for(int i=0;i< arr.length;i++)
{
arr2[i]=arr[i];
}
//display copy array
System.out.println("copy the array");
for(int i=0;i< arr.length;i++)
{
System.out.print(arr[i] + " ");
}
output
original array is:
11 2 33 4 5
copy array is:
11 2 33 4 5
Next Topic
No comments:
Post a Comment