algorithm to reverse an array code example
Example 1: reverse string array java
public class ReverseArrayDemo
{
public static void main(String[] args)
{
int[] arrNumbers = new int[]{2, 4, 6, 8, 10};
System.out.println("Given array: ");
for(int a = 0; a < arrNumbers.length; a++)
{
System.out.print(arrNumbers[a] + " ");
}
System.out.println("Reverse array: ");
for(int a = arrNumbers.length - 1; a >= 0; a--)
{
System.out.print(arrNumbers[a] + " ");
}
}
}
Example 2: c++ reverse array
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < size(arr); i++) {
int temp = arr[i];
arr[i] = arr[size(arr) - 1 - i];
arr[size(arr) - 1 - i] = temp;
}