May 20, 2015

C program to insert an integer into a given position (array)

#include<stdio.h>
#include<conio.h>
void main()
{
int i,p,x,n,a[10];
clrscr();
printf("Enter the value for n \n");
scanf("%d",&n);
printf("the values are...\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the position and element \n");
scanf("%d %d",&p,&x);
printf("before display \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=n-1;i>=p-i;i--)
a[i+1]=a[i];
a[p-1]=x;
printf(" \nAfter display \n");
for(i=0;i<=n;i++)
printf("%d \t",a[i]);
getch();
}

OUTPUT:
Enter the value for n :3
the values are...
1 2 3
enter the position & element
2  9
before display....
1 2 3
after display....
1 9 2 3

No comments:

Post a Comment