Subscribe

RSS Feed (xml)

Wednesday, July 29, 2009

Write a C program to Insert an element at the 'n' th position

#include(stdio.h)
#include(conio.h)
int main( )
{
int a[20], len, i, j, x, n ;
clrscr( );
printf("Enter the array length:");
scanf("%d", &len);
printf("\nEnter the array elements:");
for(i=0; i<'len'; i++) //remove the single quotes in for loop
scanf("%d", &a[i]);
printf("\nEnter the 'n' th position:");
scanf("%d", &n);
printf("\nEnter the array element you want to insert:");
scanf("%d", &x);
for( j= len; j>=n; j--)
{ a[ j+1]= a[ j]; }
a[ j]= x;
len=len+1;
printf("\nThe New List is :\n");
for( i=0; i<'len'; i++) //remove the single quotes in for loop
printf("%5d", a[i] );
getch( );
return 0;
}
Output:-
Enter the array length : 4
Enter the array elements: 5
8
1
9
Enter the 'n' position: 4
Enter the array element you want to insert: 10
The new List is:
5
8
1
10
9
Note:- Here 'x' is for to store the 'n' position value

1 comment:

  1. U must check the code please it is wrong
    it must be
    for(j=len-1;j>n-1;j--)
    {
    a[j+1]=a[j];
    }
    a[n-1]=x;
    len=len+1;


    By the way i like your blog

    ReplyDelete