C Program to Insert an element in Array

This page has a C Program to C Program to Insert an element in Array. It is clear that, this C program will insert a new element in array. For example: If we have entered 5 elements in an array as 35 78 43 12 46 and if we need to insert one more new element (64) in the array, then the program should ask the location (3) of the new element (64) to be inserted in the array; Then the program will accept the new element (64) and inserted in the array as 35 78 64 43 12 46. The below given C program will Insert an element in Array. Kindly check out the program to Insert an element in Array.

Program to Insert an element in Array

#include<stdio.h>

int main(){

int array1[30];

int i, loc, n1, n2;

printf("

Enter no of elements to array :");

scanf("%d", &n1);

printf("



Enter the Array elements one by one

");

for (i = 1; i<=n1; i++) {

scanf("%d", &array1[i]);

}

printf("

Enter an element to be inserted : ");

scanf("%d", &n2);

printf("

Enter a location to which the element to be inserted : ");

scanf("%d", &loc);

for (i = n1+1; i>loc; i--)

array1[i]=array1[i-1];

array1[loc]=n2;

printf("

The Array elements are :

");

for (i = 1; i<=n1+1; i++)

printf("

%d", array1[i]);

return(0);

} Sample Output: Enter no of elements to array :5

Enter the Array elements one by one

67 34 75 23 94

Enter an element to be inserted : 43

Enter a location to which the element to be inserted : 4

The Array elements are :

67

34

75

43

23

94

Related Popular Posts: