-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathreverse.java
More file actions
36 lines (33 loc) · 766 Bytes
/
reverse.java
File metadata and controls
36 lines (33 loc) · 766 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
/*The program below is used to reverse an array
* for example an array of {1,2,3,4,5}
* will become {5,4,3,2,1} upon reversing
*/
package kshitizjava;
import java.util.*;
public class reverse {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int size,i;
System.out.println("enter size of array");
size=in.nextInt();
int arr[]=new int[size];
System.out.println("enter values in the array");
for(i=0;i<size;i++)
{
arr[i]=in.nextInt();
}
int temp;
for(i=0;i<size/2;i++)
{
//swapping elements to obtain reversed array
temp=arr[i];
arr[i]=arr[size-i-1];
arr[size-i-1]=temp;
}
System.out.println("array after reversing is");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
}
}