jrliv
6/1/2017 - 12:10 AM

Example of how to reverse an array using for loops.

Example of how to reverse an array using for loops.

int[] orgArray = { 1, 2, 3, 4, 5 };
int length = orgArray.Length;
int[] revArray = new int[length];

for (int i = 0; i < length; i++)
{
  revArray[length - i - 1] = orgArray[i];
}

Console.Write("Original array: ");
for (int i = 0; i < length; i++)
{
  Console.Write(orgArray[i] + " ");
}

Console.WriteLine();
Console.Write("Reversed array: ");
for (int i = 0; i < length; i++)
{
  Console.Write(revArray[i] + " ");
}

//	Original array: 1 2 3 4 5
//	Reversed array: 5 4 3 2 1