codescracker


c

C Program to Insert Element in Array



« Previous Program Next Program »


Insert Element in Array in C

To insert element in an array in C programming, you have to ask to the user to enter the array size and array elements and ask to the user to enter the element and position where he or she want to insert the element to insert that element at the desired position

C Programming Code to Insert Element in Array

Following C program ask to the user to enter array size, then ask to the user to enter array elements, then ask to enter element or number to be insert, and at last it will ask to enter the position (index number) where he or she want to insert the desired element in the array, so this program insert the desired array and display the new array on the screen after inserting the element:

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

When the above c program is compile and executed, it will produce the following result:

c program insert element in array

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test