C Program to insert an Element in an Array

How to write a C Program to insert an element in an Array at a user-specified location using For Loop and While Loop.?

C Program to insert an Element in an Array using For Loop

This C program allows the user to enter the size, Elements of an Array, an element’s location, and the element’s value. Using the For Loop, this C program is going to insert the value or element in an Array at the user-specified location.

#include <stdio.h>
 
int main()
{
  int Array[10], Position, i, Number, Value;
 
  printf("\nPlease Enter Number of elements in an array\n");
  scanf("%d", &Number);
 
  printf("\nPlease Enter %d elements of an Array \n", Number);
  for (i = 0; i < Number; i++)
   {
     scanf("%d", &Array[i]);
   }     
 
  printf("\nPlease Enter the location of a Element you want to insert\n");
  scanf("%d", &Position);
 
  printf("\nPlease Enter the value of an Array Emenent to insert\n");
  scanf("%d", &Value);
 
  for (i = Number - 1; i >= Position - 1; i--)
   {
	     Array[i+1] = Array[i];
   }
  Array[Position-1] = Value;
  
 printf("\n Final Array after Inserting an  Elemnt is:\n");
 for (i = 0; i <= Number; i++)
  {
 	printf("%d\t", Array[i]);
  }     
 
 return 0;
}
C program to insert an element in an array using For Loop

In this c program to insert an element in an array example, the below for loop iterates each cell present in the Array[3] array. The condition inside the for loops (i < Number) ensures that the compiler will not exceed the array size. Otherwise, the array will overflow.

Please refer to Arrays in C article to understand the concept of Array size, index position, etc.

The scanf statement inside this will store the user entered values as an array element such as a[0], a[1], a[2]

The next C programming printf statement asks the user to enter the location to insert the new element. The scanf statement will assign the user entered value to the Position Variable

The Following printf statement allows entering the value to insert the at the user-defined location. And the scanf statement assigns it to the Value variable

From the above c program to insert an element in an array example screenshot, you can observe that

Size = 4 and Elements of an Array are {10, 20, 30, 40}. This means
Arr[0] = 10
Arr[1] = 20
Arr[2] = 30
Arr[3] = 40

Position of an Element = 2, and User Entered Value is 89

For Loop – First Iteration
i = Number–1 means i will initialize to 3
i=3, position = 2

Condition inside the For loop (i >= Position – 1) is True because (3 is greater than 1)
Array[i+1] = Array[i];
Array[4] = Array[3]
Array[4] = 40

After the statement is executed, i will be decremented to 1 because we used the Decrement Operator in For Loop.

C Program to insert an Element in an Array Second Iteration: After decrementing, i become 2. So, i = 2, position = 2
Condition inside the For loop (i >= Position – 1) is True because (2 is greater than 1)
Array[3] = Array[2] = 30

Third Iteration: After decrementing, i value becomes 1. So, i = 1, position = 2
Condition inside the For loop (i >= Position – 1) is True because (1 is Equal to 1)
Array[2] = Array[1] = 20

After decrementing, i become 0. It means the condition inside the For loop will fail. So, the C program compiler will exit from the for loop.

The final array of the c program to insert an element in an array program will be: Array[4] = {10, 20, 20, 30, 40}

Now, we have to insert the new element at position 2. The statement below will do the trick for you
Array[Position-1] = Value;
Array[2-1] = 89;

Next, for loop,

for (i = 0; i <= Number; i++)
  {
 	printf("%d\t", Array[i]);
  }

It Will traverse all the elements in the Array as we explained above, and it will display the values inside the Array[i] array using the printf statements array.

C Program to insert an Element in an Array using While Loop

This program allows the user to enter the array size, items, Location of an element, and the value of an element. We are using While Loop to insert the value at the user-specified location.

#include <stdio.h>
 
int main()
{
  int Array[10], Position, i, Number, Value;
 
  printf("\nPlease Enter Number of elements\n");
  scanf("%d", &Number);
 
  printf("\nPlease Enter %d elements \n", Number);
  for (i = 0; i < Number; i++)
   {
     scanf("%d", &Array[i]);
   }     
 
  printf("\nPlease Enter the location of a Element you want to insert\n");
  scanf("%d", &Position);
 
  printf("\nPlease Enter the value to insert\n");
  scanf("%d", &Value);
 
  i = Number - 1;
  while(i >= Position - 1)
   {
	 Array[i+1] = Array[i];
	 i--;
   }
 Array[Position-1] = Value;
  
 printf("\n Final after Inserting an  Element is:\n");
 for (i = 0; i <= Number; i++)
  {
 	printf("%d\t", Array[i]);
  }     
 
 return 0;
}

We just replaced the For Loop with the While Loop, but the functionality is the same as the above program to insert an element in an array example. Please refer to the While Loop in C article to understand the While Loop Functionality.

C Program to insert an Element in an Array using While Loop

Comments are closed.