HTML & JS
#include<iostream>
using namespace std;
int 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
  cout<<"The elements before insertion:";
  for(int i=0;i<n;i++)
     cout<<a[i]<<" "; 
  for(int i=n-1;i>=0;i--)
      a[i+1]=a[i];
  a[0]=front;
  n++;
  cout<<"\nThe elements after insertion at first:";
  for(int i=0;i<n;i++)
     cout<<a[i]<<" "; 
 
 //insertion at last
 int last=6;
    //element to be added at last
 a[n]=last;
 n++;
 cout<<"\nThe elements after insertion at last:";
 for(int i=0;i<n;i++)
     cout<<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;
 n++;
 cout<<"\nThe elements after insertion in middle:";
 for(int i=0;i<n;i++)
     cout<<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 6 
Report Bug