INSERTION

 Program to Insert an element in array at a given location.

#include<stdio.h>
int main()
{
    int a[100],p,i,n,j;
    printf("enter the size of array: ");
    scanf("%d",&n);
    if(n>100)
    {
        printf("(enter size less than 100)");
    }
    else
    {
        for(i=0; i<n; i++) {
            printf("enter the elements- %d: ",i+1);
            scanf("%d",&a[i]);
        }
        printf("element to be inserted : ");
        scanf("%d",&j);
        printf("location : ");
        scanf("%d",&p);
        for(i=n; i>=p; i--)
            a[i]=a[i-1];
        a[p]=j;
        for(i=0; i<=n; i++)
            printf("%2d",a[i]);
        return 0;
    }
}

OUTPUT :

enter the size of array: 5

enter the elements- 1: 9
enter the elements- 2: 7
enter the elements- 3: 0
enter the elements- 4: 2
enter the elements- 5: 5
element to be inserted : 1
location : 0
 1 9 7 0 2 5

Comments