A6 Write a program to merge two arrays and to display the merged array

    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[5];
            int[] b = new int[5];

            Console.WriteLine("Enter 5 Numbers for First Array : ");
            for (int i = 0; i < a.Length; i++)
                a[i] = int.Parse(Console.ReadLine());

            Console.WriteLine("\nEnter 5 Numbers for Second Array : ");
            for (int i = 0; i < b.Length; i++)
                b[i] = int.Parse(Console.ReadLine());

            int[] c = new int[a.Length + b.Length];
            int k = 0;
    
            for (int i = 0; i < a.Length; i++, k++)
            {
                c[k] = a[i];
            }
            for (int i = 0; i < b.Length; i++, k++)
            {
                c[k] = b[i];
            }

            Console.WriteLine("\nThe elements of the merged array :");
            for (int i = 0; i < c.Length; i++)
            {
                Console.Write(c[i] + " ");
            }

            Console.ReadLine();
        }

    }

Output : 


No comments:

Post a Comment