Program Insert element in an Array

December 29, 2024 No Comments »





// -------------------------------------------------------
// x   - element to be inserted
// n   - no.of elements in the array
// i   - for scanning of the array
// loc - place where the new element is to be inserted  
//---------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[30],x,n,i,loc;
 printf("\n Enter no of elements :");
 scanf("%d",&n);
 /* read n elements in an array  */
 for(i=0 ; i < n ; i++)
  scanf("%d",&a[i]);
 printf("\n Enter the element to be inserted :");
 scanf("%d",&x);
 printf("\n Enter the location");
 scanf("%d",&loc);
 /* create space at the specified location */
 for(i = n-1 ;i <= loc-1 ; i--)
  a[i+1]=a[i];
 n++;
 a[loc-1]=x;      // Element Inserted Here
  /* Print out the Result of Insertion */
        for(i = 0 ;i< n ;i++)
        printf("\n %d",a[i]);
}

Download Program

Leave A Response

Predefined Skins

BG Color

Nav Menu Font

Titles font

Content font