-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_an_element.java
More file actions
37 lines (28 loc) · 969 Bytes
/
delete_an_element.java
File metadata and controls
37 lines (28 loc) · 969 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
import java.util.*;
public class delete_an_element{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("enter size of array - ");
int n=sc.nextInt();
int a[] = new int[n];
System.out.print("enter elements - ");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print("enter position where you want to delete - ");
int k = sc.nextInt();
if (k > 0 && k <= n) {
int item = a[k - 1];
for (int j = k - 1; j < n - 1; j++) {
a[j] = a[j + 1];
}
n--;
System.out.println("Updated array after deleting element at position " + k + ":");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
} else {
System.out.println("deletion not possible");
}
}
}