Tutorials

Empowering academic aid to discover, learn and succeed

C Program to insert an element into specified location of an array


Posted by: admin

Program


#include<stdio.h>
 
void main()
{
	int arr[30], element, num, i, location;
	
	printf("Enter no of elements:\t");
	scanf("%d", &num);
	
	printf("Enter the values to array:\n");
	for (i = 0; i < num; i++)
	{
		scanf("%d", &arr[i]);
	}
 
	printf("Enter the element to be inserted:\t");
	scanf("%d", &element);
 
	printf("Enter the location:\t");
	scanf("%d", &location);
	
	for (i = num; i >= location; i--)
	{
		arr[i] = arr[i - 1];
	}
 
	num++;
	arr[location - 1] = element;
	
	for (i = 0; i < num; i++)
		printf("%d\n", arr[i]);
}

Output


Enter no of elements:	5
Enter the values to array:
12
45 
56
86 
12
Enter the element to be inserted:	59 
Enter the location:	5
12
45
56
86
59
12


Likes :

Tags:
  • Arrays