-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram33.java
More file actions
37 lines (28 loc) · 994 Bytes
/
Program33.java
File metadata and controls
37 lines (28 loc) · 994 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
// Program 33
// Week 1 - Day 6
// Array insertion by not removing any existing elements.
import java.util.*;
public class Program33 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int n = sc.nextInt();
int[] arr = new int[n + 1];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Enter the position to insert the element:");
int position = sc.nextInt();
System.out.println("Enter the element to be inserted:");
int element = sc.nextInt();
for (int i = n; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
System.out.println("Array after insertion:");
for (int i = 0; i <= n; i++) {
System.out.print(arr[i] + " ");
}
}
}