C Program to Insert element in an Array

C Program to Insert element in an Array:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().



#include<stdio.h>
main()
{
   int arr[50],element,num,i,loc;

   printf("Enter no of elements to be entered in array : ");
   scanf("%d",&num);

   printf("Enter %d elements :",num);
   for(i=0;i<num;i++)
    {
      scanf("%d",&arr[i]);
    }

   printf("\nEnter the element to be inserted in array : ");
   scanf("%d",&element);

   printf("\nEnter the location where you want to insert element : ");
   scanf("%d",&loc);

   for(i=num;i>=loc;i--)
    {
      arr[i]=arr[i-1];
    }
   num++;
   arr[loc-1]=element;

   //Printing result
   for(i=0;i<num;i++)
      printf("%d\t",arr[i]);
}

OUTPUT:

Enter no of elements to be entered in array:5
Enter 5 elements :7 1 3 2 4

Enter the element to be inserted in array:10

Enter the location where you want to insert element :3
7       1       10      3       2       4

 

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × one =

This site uses Akismet to reduce spam. Learn how your comment data is processed.