Insertion Operation on Array

July 27, 2022 · Team EasyExamNotes · 1 min read (est.) · 0 comments

Share on WhatsApp

In an array, insertion is the process of inserting one or more elements.

In an array, insertion can be performed at

  • Before first element
  • After last element
  • At any place

Insertion operation, before first element:

#include<stdio.h>

int main()

{

    int arr[5], n, i, element;

    printf("Enter the size of array\n");
    scanf("%d", &n);

    printf("\nEnter %d Elements in array\n",n);
    
        for(i=0;i<5;i++)
        {
         scanf("%d", &arr[i]);
        }

    printf("\nenter the element at the beginning\n");
    scanf("%d", &element);
    n++;

        for(i=n; i>1; i--)
        {
        arr[i-1]=arr[i-2];
        }

    arr[0]=element;
    printf("After insertion, array elements");

        for(i=0;i<n;i++)
        {
        printf("\n%d", arr[i]);
        }

    return 0;
}

Output

Discussion

Ask a question, share your thoughts or report an error.

Sign in to participate in the discussion.

Join the discussion