HTML & JS
//pgm for insertion operations
#include<stdio.h>
void main()
{
  //declaring and initializing array
  int a[10]={1,4,2};
  int n=3;     //element count

  //insertion at front
  int front=89;   //value to be added at front
  printf("The elements before insertion:");
  for(int i=0;i<n;i++)
     printf("%d ",a[i]); 
  for(int i=n-1;i>=0;i--)
      a[i+1]=a[i];
  a[0]=front;
  n++;
  printf("\nThe elements after insertion at first:");
  for(int i=0;i<n;i++)
     printf("%d ",a[i]); 
 
 //insertion at last
 int last=6;
    //element to be added at last
 a[n]=last;
 n++;
 printf("\nThe elements after insertion at last:");
 for(int i=0;i<n;i++)
     printf("%d ",a[i]); 

 //insertion at a index
 int middle=55;    //element to be added in middle
 int index=2;     //index value
 for(int i=n-1;i>=index;i--)
    a[i+1]=a[i];
 a[index]=middle;
 printf("\nThe elements after insertion in middle:");
 for(int i=0;i<n;i++)
     printf("%d ",a[i]); 
}

Generated URL:

Output:

The elements before insertion:1 4 2 
The elements after insertion at first:89 1 4 2 
The elements after insertion at last:89 1 4 2 6 
The elements after insertion in middle:89 1 55 4 2 
Report Bug