-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseArray.java
More file actions
43 lines (32 loc) · 850 Bytes
/
Copy pathReverseArray.java
File metadata and controls
43 lines (32 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package Array;
import java.util.Scanner;
public class ReverseArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array :");
int size = sc.nextInt();
int arr[] = new int[size];
System.out.print("Enter the array elements :");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
// logic
int temp;
for (int i = 0; i < size / 2; i++) {
// swapping using third variable
/*
* temp = arr[i];
* arr[i] = arr[size - 1 - i];
* arr[size-1-i]= temp;
*/
// swapping without third variable
arr[i] = arr[i] + arr[size - 1 - i];
arr[size - 1 - i] = arr[i] - arr[size - 1 - i];
arr[i] = arr[i] - arr[size - 1 - i];
}
// Print the array
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
}
}