# Reverse elements of an array

Abhishek Kumar Category: Data StructuresTags: datastructures array reverse

Write a program to input elements in an array and find reverse of an array.
So in this post, we are going to learn, how to find reverse of an array in C programming, Python, Java and JavaScript and logic to find reverse of an array in C program.

##### Example -
```    Input: 1, 2, 3, 4, 5
Output: 5, 4, 3, 2, 1

Input: 3, 2, 5, 9, 7, 8
Output: 8, 7, 9, 5, 2, 3```

#### Iterative solution:

1. Take input values into an array
2. Loop through the array from `index=0` to `size/2` and swap `arr[index]` from `arr[size-1-index]`
3. Then, print the array
``````num = int(input('Enter number of array elements: '))
arr = []
print('Enter array elements -')
for _ in range(num):
arr.append(int(input()))

for i in range(len(arr) // 2):
arr[i], arr[len(arr)-i-1] = arr[len(arr)-i-1], arr[i]

print('After reversing -')
print(arr)``````
``````import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter number of elements: ");
int num = sc.nextInt();

int[] arr = new int[num];

System.out.println("Enter array elements -");

for (int i = 0; i < num; i++)
arr[i] = sc.nextInt();

for (int i = 0; i < arr.length / 2; i++) {
int temp;
temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}

System.out.println("After reversing -");
for (int i = 0; i < num; i++)
System.out.println(arr[i]);
}
}``````
``````#include <stdio.h>

void main(void) {
int arr[5], n=5, i, temp;

printf("Enter array elements\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

for (i = 0; i < n / 2; i++)
{
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}

printf("\nAfter reversing\n");
for (i = 0; i < n; i++)
printf("%d\n", arr[i]);
}``````
``````arr = [1, 2, 3, 4, 5];

for (let i = 0; i < arr.length/2; i++) {
// Destructuring Assignment Array
[arr[i], arr[arr.length-i-1]] = [arr[arr.length-i-1], arr[i]]
}

console.log(arr);``````

Output

```Enter number of array elements: 5
Enter array elements -
1
2
3
4
5
After reversing -
[5, 4, 3, 2, 1]```

AUTHOR

### Abhishek Kumar

Software engineer | Blogger | Keen learner
Python | Django | JavaScript | React | Next.js | Express.js | C
Passionate about learning and experimenting with new things and open to more opportunities and collaborations.