forked from SaketJNU/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseArray.java
More file actions
41 lines (40 loc) · 961 Bytes
/
ReverseArray.java
File metadata and controls
41 lines (40 loc) · 961 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
//Program that reverses an array and stores it in the same array.
import java.util.Scanner;
class ReverseArray
{
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
System.out.print("\nEnter the size of array: ");
int size = reader.nextInt();
int[] arr = new int[size];
System.out.print("\nEnter the array -----------------");
for (int i = 0; i<size; i++)
{
System.out.print("\n Enter the " + (i+1) + " element: ");
arr[i] = reader.nextInt();
}
System.out.print("\nEntered array is: ");
for(int i=0; i<size;i++)
{
System.out.print("\t" + arr[i]);
}
// Code to Reverse the array
int start = 0;
int end = size-1;
int temp = 0;
while(start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start = start + 1;
end = end - 1;
}
System.out.print("\nReverse array is: ");
for(int i=0; i<size;i++)
{
System.out.print("\t" + arr[i]);
}
}
}