Thursday 26 January 2012

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

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20], len, i, j, x, n ;
    clrscr( );
    printf("Enter the array length:");
    scanf("%d", &len);
    printf("\nEnter the array elements:\n");
    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-1;j>=n-1;j--)
    {
        a[j+1]= a[j];
    }
    a[n-1]= 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( );
}

 
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

No comments:

Post a Comment