Insert element in an array C program

It’s the insert element in an array C program that inputs numbers from the user, and stores these numbers in an array, the user can add an element to the given array, and print array on the screen.

To understand this program, it’s better you should know the following topics:

Program (1): to insert an element into the array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,p,k;
printf("Enter the number of elements(upto 100) to be entered in the list : ",n);
scanf("%d",&n);
//accepting values into array
for(i=0;i<n;i++)
{
 printf("Enter %d element : ",i+1);
 scanf("%d",&a[i]);
}
//position of element to be add into an array
printf("\nEnter the position (1-%d) and element to be add : \n",n);
scanf("%d %d",&p,&k);
//adding an element
for(i=n-1;i>=p-1;i--)
{
 a[i+1]=a[i];
}
a[p-1]=k;
printf("\nElement %d is added at the position %d \n",k,p);
//Printing an array a elements after adding element
printf("\nThe New list\n");
for(i=0;i<n+1;i++)
    {
    printf("%d\t",a[i]);
    }
getch();
}

Output (1)

Enter the number of elements(upto 100) to be entered in the list : 4
Enter 1 element : 2
Enter 2 element : 1
Enter 3 element : 4
Enter 4 element : 7

Enter the position (1-4) and element to be add :
3
10

Element 10 is added at the position 3

The New list
2 1 10 4 7

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *