C Program to Insert Element in Array

#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int array[50], size, element, i, position;
    printf("Enter Array Size : ");
    scanf("%d",&size);
    printf("Enter array elements : ");
    for(i=0; i<size; i++)
    {
        scanf("%d",&array[i]);
    }
    printf("Enter element to be insert : ");
    scanf("%d",&element);
    printf("At which position (Enter index number) ? ");
    scanf("%d",&position);
    // now create a space at the required position
    for(i=size; i>position; i--)
    {
        array[i]=array[i-1];
    }
    array[position]=element;
    printf("Element inserted successfully..!!\n");
    printf("Now the new array is : \n");
    for(i=0; i<size+1; i++)
    {
        printf("%d ",array[i]);
    }
    getch();
}

Run:

No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...