C Program to Insert an Element in an Array

Here is the basic C program to embed a component in a one-dimensional exhibit.

The cluster ought to be in climbing request.

Program

#include<stdio.h>
 
int main()
{
	int a[20],n,x,i,pos=0;
	printf("Enter size of array:");
	scanf("%d",&n);
	printf("Enter the array in ascending order:\n");
	
	for(i=0;i<n;++i)
		scanf("%d",&a[i]);
	
	printf("\nEnter element to insert:");
	scanf("%d",&x);
	
	for(i=0;i<n;++i)
		if(a[i]<=x&&x<a[i+1])
		{
			pos=i+1;
			break;
		}
		
	for(i=n+1;i>pos;--i)
		a[i]=a[i-1];
	
	a[pos]=x;
	printf("\nArray after inserting element:");
	
	for(i=0;i<n+1;i++)
		printf("%d ",a[i]);
 
	return 0;
}

Output

Enter size of array:6
Enter the array in ascending order:
1 2 4 5 6 7

Enter element to insert:3

Array after inserting element:1 2 3 4 5 6 7

Leave a Comment

error: Alert: Content is protected!!