To insert a given number in the array using C


Levels of difficulty: / perform operation:

C a program to insert a given number in the array in a given position.

C Program

#include <stdio.h>
#include <conio.h>
void main()
{
	int a[5],i,j=5,k,val,pos;
	clrscr();
	for(i=0;i<5;i++)
        {
		printf("\nENTER NUMBER-%d: ",i+1);
		scanf("%d",&a[i]);
	}
	printf("\nENTER VALUE TO INSERT: ");
	scanf("%d",&val);
	printf("\nENTER POSITION TO INSERT: ");
	scanf("%d",&pos);
	j++;
	for(k=j;k>=pos;k--)
        {
		a[k]=a[k-1];
	}
	a[--pos]=val;
	printf("\nTHE ARRAY IS: ");
	for(i=0;i<j;i++)
        {
		printf("%d\t",a[i]);
	}
	getch();
}


Output







Quantitative Aptitude
Reasoning
Programming
Interview