About Me

header ads

Program to insert an element in array at user defined position.

Program to insert an element in array at user defined position.

#include <stdio.h>
#include<conio.h>
void main()
{
int a[100], position,i,n,value;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d elements\n", n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d",&position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for(i=n-1;i>=position-1;i--)
a[i+1]=a[i];
array[position-1] = value;
printf("Resultant array is\n");
for(i=0;i<=n;i++)
printf("%d\n",a[i]);
getch();
}

OUTPUT:
Enter number of elements in array
5
Enter 5 elements
1
2
4
5
78
Enter the location where you wish to insert an element
3
Enter the value to insert
89
Resultant array is
1
2
89
4
5
78