-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInsertAtAnyPosition.c
39 lines (31 loc) · 936 Bytes
/
InsertAtAnyPosition.c
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
#include <stdio.h>
int main() {
int array[10] = {500, 1000, 1500, 2000, 2500};
int newElement, position, i;
// Print the original array
printf("Original Array: ");
for (i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
// Input the new element and position
printf("\nEnter the new element: ");
scanf("%d", &newElement);
printf("Enter the position to insert: ");
scanf("%d", &position);
if (position < 0 || position > 5) {
printf("Invalid position!\n");
return 1;
}
// Shift elements to the right to make space for the new element
for (i = 4; i >= position; i--) {
array[i + 1] = array[i];
}
// Insert the new element at the specified position
array[position] = newElement;
// Print the updated array
printf("Arrey After Updated: ");
for (i = 0; i < 6; i++) {
printf("%d ", array[i]);
}
return 0;
}