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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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