-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdtAdd.java
More file actions
33 lines (25 loc) · 755 Bytes
/
Copy pathAdtAdd.java
File metadata and controls
33 lines (25 loc) · 755 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
import java.util.*;
public class AdtAdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] num = new int[n];
for (int i = 0; i < n; i++) {
num[i] = sc.nextInt();
}
System.out.println("Enter the Element to add");
int k = sc.nextInt();
// create new array with size n+1
int[] newArr = new int[n + 1];
// copy old elements
for (int i = 0; i < n; i++) {
newArr[i] = num[i];
}
// insert new element at end
newArr[n] = k;
// print new array
for (int i = 0; i < n + 1; i++) {
System.out.print(newArr[i] + " ");
}
}
}