-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.c
More file actions
57 lines (42 loc) · 1.14 KB
/
36.c
File metadata and controls
57 lines (42 loc) · 1.14 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
47
48
49
50
51
52
53
54
55
56
57
// Program to insert an element in array (by position wise/by element wise).
#include <stdio.h>
int main() {
int a[50], n, i, choice, pos, value;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("1. Insert by position\n");
printf("2. Insert at end\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter position: ");
scanf("%d", &pos);
printf("Enter value to insert: ");
scanf("%d", &value);
for (i = n; i >= pos; i--) {
a[i] = a[i - 1];
}
a[pos - 1] = value;
n++;
break;
case 2:
printf("Enter value to insert: ");
scanf("%d", &value);
a[n] = value;
n++;
break;
default:
printf("Invalid choice");
return 0;
}
printf("Array after insertion:\n");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}