-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicate elements.java
More file actions
38 lines (31 loc) · 1.14 KB
/
duplicate elements.java
File metadata and controls
38 lines (31 loc) · 1.14 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
import java.util.Scanner;
public class ReverseArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the size of the array
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
// Create the array with the specified size
int[] array = new int[size];
// Prompt the user to enter the elements of the array
System.out.println("Enter " + size + " elements for the array:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
array[i] = scanner.nextInt();
}
// Reverse the array in place
for (int i = 0; i < size / 2; i++) {
// Swap elements
int temp = array[i];
array[i] = array[size - 1 - i];
array[size - 1 - i] = temp;
}
// Print the reversed array
System.out.println("Reversed array:");
for (int i = 0; i < size; i++) {
System.out.print(array[i] + " ");
}
// Close the scanner
scanner.close();
}
}