By using the below c program you can insert an element into the current array, on which position you want. First, you need to create the array, then enter the element of an array and after that enter the element and the position number where you want to insert that element into the created array.
This c program is very simple & easy and it will automatically insert that element in that position. See the below example.
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 | #include<stdio.h> int main() { int arr[30], element, num, i, location; printf("\nEnter no of elements :"); scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } printf("\nEnter the element to be inserted :"); scanf("%d", &element); printf("\nEnter the location"); scanf("%d", &location); //Create space at the specified location for (i = num; i >= location; i--) { arr[i] = arr[i - 1]; } num++; arr[location - 1] = element; //Print out the result of insertion for (i = 0; i < num; i++) printf(" %d", arr[i]); return (0); } |
OUtput is:
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: array, array in c, array push in c, arrays in c, what are arrays in c, what is array in c