C Program to insert an element in an array
In this program, You will learn how to insert an element in an array in c.
List is: 1 2 3 4 5 6
Insert item: 44
Insert location: 7
New list is: 1 2 3 4 5 6 44
Example: How to insert an element in an array in c.
#include<stdio.h>
int main() {
int arr[10], n, i, num, loc;
printf("Enter size of an array:");
scanf("%d", &n);
printf("Enter array elements:");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element for insert:");
scanf("%d", &num);
printf("Enter location :");
scanf("%d", &loc);
for (i = n; i >= loc; i--) {
arr[i] = arr[i - 1];
}
n++;
arr[loc - 1] = num;
printf("After insert array list:");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter size of an array:5
Enter array elements:2 3 4 5 6
Enter element for insert:88
Enter location :2
After insert array list:2 88 3 4 5 6