WAP to insert an element into array.

//WAP to isert an element into array.

#include<stdio.h>
#include<conio.h>
void main()
{
	char ch='y';
	int n,k,a[100],r,i;
	clrscr();
	printf("\nHow many number:");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("\nElement %d:",i+1);
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++)
	{
		printf("\n%d",a[i]);
	}
	while(ch=='y')
	{
		printf("\nEnter position where you want to enter:");
		scanf("%d",&k);
		printf("\nEnter new number:");
		scanf("%d",&r);
		for(i=n-1;i>=k-1;i--)
		{
			a[i+1]=a[i];
		}
		a[k-1]=r;
		n=n+1;
		for(i=0;i<n;i++)
		{
			printf("\n%d",a[i]);
		}
		printf("\nDo you want to continue y or n:");
		fflush(stdin);
		scanf("%c",&ch);
	}
	getch();
}

No comments:

Post a Comment