-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram39.java
More file actions
46 lines (34 loc) · 1.05 KB
/
Copy pathprogram39.java
File metadata and controls
46 lines (34 loc) · 1.05 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
// insertion of array elements :
import java.util.Scanner;
public class program39{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
int n = 5;
System.out.print("Original Array: ");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.print("\nEnter Position (0 to " + n + "): ");
int pos = sc.nextInt();
System.out.print("Enter Element to Insert: ");
int element = sc.nextInt();
// Shift elements to the right
for (int i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
// Insert new element
arr[pos] = element;
n++;
System.out.print("Array After Insertion: ");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
sc.close();
}
}