Adding number at a specific position in an array in C
#include<stdio.h>
int main()
{
int i,n,arr[20],num,pos;
printf("\nEnter size of array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n arr[%d]= ",i);
scanf("%d",&arr[i]);
}
printf("\nEnter no want to add \n");
scanf("%d",&num);
printf("\nEnter position \n ");
scanf("%d",&pos);
for(i=n-1;i>pos;i--)
{
arr[i+1]=arr[i];
}
arr[pos]=num;
n=n+1;
printf("\n the after insertion is %d",num);
for(i=0;i<n;i++)
{
printf("\n arr[%d]=%d",i,arr[i]);
}
return 0;
}
output:
Comments
Post a Comment