C# program to reverse an array elements

In this C# program, we will reverse an array without using Array.Revers() Method.

C# program to reverse an array elements Code:

private static void Main(string[] args)
        {
            int[] array = { 7, 6, 4, 5, 2, 1, 3, 8 };

            Console.Write("Initial Array: ");
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write("{0},", array[i]);
            }
                
            //---- Reverse Array
            for (int i = 0; i < array.Length / 2; i++)
            {
                int tmp = array[i];
                array[i] = array[array.Length - i - 1];
                array[array.Length - i - 1] = tmp;
            }

            Console.Write("\nReversed Array: ");
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write("{0},", array[i]);
            }
            Console.ReadLine();
        }

C# program to reverse an array elements Output:

CSharp-program-to-reverse-an-array-elements

 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice