-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Insertion_At_Any_Position.java
More file actions
44 lines (41 loc) · 1.07 KB
/
Array_Insertion_At_Any_Position.java
File metadata and controls
44 lines (41 loc) · 1.07 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
import java.util.Scanner;
public class Array_Insertion_At_Any_Position
{
public static void main(String[] args)
{
int n,m,p;
Scanner sc = new Scanner(System.in);
System.out.println("enter no. of elements in array");
n=sc.nextInt();
int a[]= new int[n];
int b[]= new int[n+1];
System.out.println("enter values");
for (int i = 0; i < n; i++)
{
a[i]= sc.nextInt();
}
System.out.println("enter position where value to be inserted");
m=sc.nextInt();
System.out.println("enter new value to be inserted");
p=sc.nextInt();
for (int i = 0; i < (n+1); i++) {
if (i<(m-1))
{
b[i]= a[i];
}
else if (i==(m-1))
{
b[i]=p;
}
else
{
b[i]= a[i-1];
}
}
System.out.println("elements are:");
for (int i = 0; i < (n+1); i++)
{
System.out.println(b[i]);
}
}
}