-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse_the_array.java
More file actions
56 lines (43 loc) · 1.43 KB
/
Reverse_the_array.java
File metadata and controls
56 lines (43 loc) · 1.43 KB
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
48
49
50
51
52
53
54
55
56
//
//
//Reversing an array in java can be done in three simple methods.
// Examples:
//
// Input : 1, 2, 3, 4, 5
// Output :5, 4, 3, 2, 1
//
// Input : 10, 20, 30, 40
// Output : 40, 30, 20, 10
package Learning;
import java.util.Scanner;
class Reverse_the_array {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Number of Element to be present in the array");
int n=sc.nextInt();
int myarr[]=new int[n];
int reversearr[]=new int[n]; // For reverse array
//*********************** Loop for inserting element in the array*****************************
for(int i=0;i<n;i++)
{
System.out.println("Enter the Element to be inserted:");
myarr[i]=sc.nextInt();
}
//************************************* Loop to display the Elements**************************************
for(int i=0;i<n;i++)
{
System.out.printf("arr[%d]=%d \n ",i,myarr[i]);
}
int j=n-1;
for(int i=0;i<n;i++){
reversearr[i]=myarr[j];
j=j-1;
}
// "*************************Reverse Array Printing *******************")
System.out.println("*************************Reverse Array*******************");
for(int i=0;i<n;i++)
{
System.out.printf("arr[%d]=%d \n ",i,reversearr[i]);
}
}
}