C Program to insert an Element in an Array

C Program to insert an Element in an Array

C program to insert or add an elements in an array; Through this tuorial, we will learn how to add or insert elements in an array using for and while loop in c programs.

Programs to insert an Element in an Array in C

Let’s use the following programs to add or insert elements in an array using for and while loop in c:

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

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

#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;
}

The output of the above c program; as follows:

Please Enter Number of elements in an array
5
Please Enter 5 elements of an Array 
1 2 3 4 5
Please Enter the location of a Element you want to insert
5
Please Enter the value of an Array Emenent to insert
10
Final Array after Inserting an  Elemnt is:
1	2	3	4	10	5	

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

#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;
}

The output of the above c program; as follows:

Please Enter Number of elements
5
Please Enter 5 elements 
1 2 3 4 5
Please Enter the location of a Element you want to insert
6
Please Enter the value to insert
45
Final after Inserting an  Element is:
1	2	3	4	5	45	

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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