-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReverseArray.java
More file actions
47 lines (35 loc) · 939 Bytes
/
Copy pathReverseArray.java
File metadata and controls
47 lines (35 loc) · 939 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
44
45
46
47
// Ques 1. Write a program to reverse an array
import java.util.Scanner;
public class ReverseArray {
static void rvereseArray(int arr[],int start, int end){
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static void printArray(int arr[],int size){
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//Given an array, the task is to reverse the array/string.
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
int [] arr= new int[n];
for (int i = 0; i < n; i++) {
arr[i]=s.nextInt();
}
rvereseArray(arr,0,n-1);
System.out.println("Reversed array-");
printArray(arr,n);
s.close();
}
}