Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Copy the elements one array into another array

C# Sharp Array: Exercise-4 with Solution

Write a program in C# Sharp to copy the elements one array into another array.

C# Sharp: Copy the elements one array into another array

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise4  
{  
    public static void Main() 
{
	int[] arr1 = new int[100];
	int[] arr2 = new int[100];
    int i, n;
	
	
       Console.Write("\n\nCopy the elements one array into another array :\n");
       Console.Write("----------------------------------------------------\n");
	   
       Console.Write("Input the number of elements to be stored in the array :");
	   n = Convert.ToInt32(Console.ReadLine()); 
   
       Console.Write("Input {0} elements in the array :\n",n);
       for(i=0;i<n;i++)
        {
	      Console.Write("element - {0} : ",i);
		  arr1[i] = Convert.ToInt32(Console.ReadLine());  
	    }
    /* Copy elements of first array into second array.*/ 
    for(i=0; i<n; i++)
    {
        arr2[i] = arr1[i];
    }

    /* Prints the elements of first array   */
    Console.Write("\nThe elements stored in the first array are :\n");
    for(i=0; i<n; i++)
    {
        Console.Write("{0}  ", arr1[i]);
    }

    /* Prints the elements copied into the second array. */
    Console.Write("\n\nThe elements copied into the second array are :\n");
    for(i=0; i<n; i++)
    {
        Console.Write("{0}  ", arr2[i]);
    }
	       Console.Write("\n\n");
  }
}

Sample Output:

Copy the elements one array into another array :                                                              
----------------------------------------------------                                                          
Input the number of elements to be stored in the array :2                                                     
Input 2 elements in the array :                                                                               
element - 0 : 2                                                                                               
element - 1 : 4                                                                                                 
The elements stored in the first array are :                                                                  
2  4                                                                                                       
The elements copied into the second array are :                                                               
2  4  

Flowchart:

Flowchart: Copy the elements one array into another array

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to find the sum of all elements of array.
Next: Write a program in C# Sharp to count a total number of duplicate elements in an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.