About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Monday, September 2, 2013

C Program to Insert an element in an Array

#include

int main()
{
 int arr[30],element,num,i,location;
 printf("n Enter no of elements :");
 scanf("%d",&num);

 for(i=0 ; i < num ; i++)
  scanf("%d",&arr[i]);

 printf("n Enter the element to be inserted :");
 scanf("%d",&element);
 printf("n Enter the location");
 scanf("%d",&location);

 /* create space at the specified location */
 for(i = num ;i >= location ; i--)
  arr[i] = arr[i-1];

 num++;
 arr[location-1] = element;

 /* Print out the Result of Insertion */
 for(i = 0 ;i < num ;i++)
        printf("n %d",arr[i]);

return(0);
}

Output of the Program :

Enter no of elements : 5
 1 2 3 4 5
 Enter the element to be inserted : 6
 Enter the location : 2
 1 6 2 3 4 5

No comments: