Insert a new element in Array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,k,new_element;
clrscr();
printf(“\n Enter the number of elements = “);
scanf(“%d”,&n);
printf(” Enter %d elements = “,n);
for(i=1;i<=n;i++) { scanf(“%d”,&a[i]); } printf(” Enter the new element to be inserted = “); scanf(“%d”,&new_element); printf(” Where %d is to be inserted = “,new_element); scanf(“%d”,&j); k=n; while(k>=j)
{
a[k+1]=a[k];
k–;
}
a[j]=new_element;
n=n+1;
printf(” Array after insertion is = “);
for(i=1;i<=n;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}

Explanation of Program :
1. We take an array with name A
.
2 We take second variable with name n which store the total number of elements .
3 we take another variable i ,k for looping control .                                      4  Print message for user to enter the total number of elements .            5  Take user’s input and convert it into int and assign to n .
6 Print message for user to enter the value of all elements .
7 Take user’s input and store it in array A .

8 Print message for user to enter the value of new element .                     9 Take user’s input and convert it into int and assign to new element . 10 Print message for user to enter the value location of new element. 11 Take user’s input and convert it into int and assign to j . 12 Print result to user on Output Screen .

Output :

Posted in: C

Leave a Reply

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