Write C# program to copy all elements of one array to another

Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
class Program
{
    static void Main()
    {
        int[] arr = new int[100];
 
        int[] first = new int[100];
        int[] second = new int[100];
 
        int i, num;
 
        //Reads size of the array
        Console.WriteLine("Enter size of the array: ");
        num = Convert.ToInt32(Console.ReadLine());
        //Reads elements in array
        Console.WriteLine("Enter elements in the array: ");
        for (i = 0; i < num; i++)
        {
            first[i] = Convert.ToInt32(Console.ReadLine());            
        }
 
        //Copy all elements from first array to second array
        for (i = 0; i < num; i++)
        {
            second[i] = first[i];
        }
 
        //Printing all elements of first array entered by user
        Console.WriteLine("Elements of first array are:");
        for (i = 0; i < num; i++)
        {
            Console.Write(first[i]+"\t");            
        }
 
        //Printing all elements of second array
        Console.WriteLine("\nElements of second array are:");        
        for (i = 0; i < num; i++)
        {
           Console.Write(second[i]+ "\t");
        }
 
 
        Console.ReadLine();
    }
 
}

Result

Write C# program to copy all elements of one array to another
Write C# program to copy all elements of one array to another

Leave a Comment