Insert element in an array

Write a program to insert an element in an array.


Source code:

#include<stdio.h>
#include<conio.h>
void main()
{
	int a[20],n,i,p,c,k,m;
	clrscr();
	printf("Enter size of array : ");
	scanf("%d",&n);
	printf("Enter array elements : \n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	printf("Enter the position for inserting new elements : ");
	scanf("%d",&p);
	printf("Enter the element to be inserted :");
	scanf("%d",&c);
	for(i=n;i>p-1;i--)
	{ 
		a[i]=a[i-1];
	}
	a[p-1]=c;
	printf("New array : \n");
	for(i=0;i<=n;i++)
	{
		printf("%d\n",a[i]);
	}
	getch();
}

Output:

Enter size of array: 3
Enter array elements:
1
2
4
Enter the position for inserting new elements: 3
Enter the element to be inserted: 3
New Array:
1
2
3
4

Leave a Reply

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