# C Program to Insert An Element in Array At Desired Position

There we will see that how to insert an element in array at desired position.
First we have to create an array with finite number of element and insert some value in it.

For example lets create an array with five elements.

``````arr[0] = 4
arr[1] = 5
arr[2] = 12
arr[3] = 3
arr[4] = 19``````

Now we want to insert an element at third position. then we have to read an element(which is 20) from user and put it at third position. and remaining element after third position will shift at next index position like this.

``````arr[0] = 4
arr[1] = 5
arr[2] = 20
arr[3] = 12
arr[4] = 3
arr[5] = 19``````

### Let’s have a look on the algorithm and the code.

``````ALGORITHM:-

step 1 : Start
step 2 : Enter size of array
step 3 : Input element in array from user
step 4 : Now Enter the new element and index position where you want to insert
step 5 : Compare the new index position with total number of elements and
insert it at the new index position
step 6 : Increase the size of array by one
step 7 : Display the total array elements
step 8 : Stop execution
``````

``````PROGRAM CODE:-

#include<stdio.h>
#include<conio.h>

void main() {
int ar[30], ele, num, i, loc;
printf("nEnter size of array :");
scanf("%d", &num);
printf("Enter Element in array: ");
for (i = 0; i < num; i++) {
scanf("%d", &ar[i]);
}
printf("nEnter the new element to be inserted :");
scanf("%d", &ele);

printf("nEnter the location");
scanf("%d", &loc);

for (i = num; i >= loc; i--) {
ar[i] = ar[i - 1];
}
ar[loc - 1] = ele;

printf("Array after element insertion: n");
for (i = 0; i <= num; i++) {
printf("n %d", ar[i]);
}

getch();
}``````

``````OUTPUT:-

Enter the size of array: 4
Enter element in array:
5
1
9
5

Enter the new element to be inserted:
123
Enter the position: 2
Array after element insertion:
5  123  1  9  5``````

``````SIMILAR ITEMS:-

C Program to insert an array at desired index
how to insert element in array at specified location
Insert element at specified location in c
Write a c program to insert at element at desired location in array``````