Reverse an Array Elements:

Write a program to Reverse an Array Elements without using In-build function.


For example:

Input:
Arr = { 100, -3, 95, 100, 95, 177, -5, -4, 177, 101 }

Output:
101, 177, -4, -5, 177, 95, 100, 95, -3, 100

Solution:

using System;
using System.Linq;

namespace Reverse
{
    class Program
    {
        static void Main()
        {

            var arr = new[] { 100, -3, 95, 100, 95, 177, -5, -4, 177, 101 };

            for (var i = 0; i < arr.Length / 2; i++)
            {
                var tmp = arr[i];
                arr[i] = arr[arr.Length - i - 1];
                arr[arr.Length - i - 1] = tmp;
            }

            Console.Write($"Reverse array: {string.Join(", ",arr)}");
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *