A3 Write a program to reverse an array

    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[5];
            int i, j, temp;

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

            for (i = 0, j = a.Length - 1; i <= j; i++, j--)
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }

            Console.WriteLine("\nThe reverse array is : ");
            for (i = 0; i < a.Length; i++)
                Console.WriteLine(a[i]);

            Console.ReadLine();
        }

    }

Output : 


No comments:

Post a Comment