[et_pb_section fb_built=”1″ _builder_version=”4.0.9″][et_pb_row _builder_version=”4.0.9″][et_pb_column type=”4_4″ _builder_version=”4.0.9″][et_pb_text _builder_version=”4.4.2″ text_font=”Verdana||||||||” text_text_color=”#000000″ text_font_size=”15px” hover_enabled=”0″]

C program to insert an element

 

[/et_pb_text][et_pb_text _builder_version=”4.4.0″ text_font=”Verdana||||||||” text_text_color=”#000000″ text_font_size=”15px”]

C programming to insert an element into an Array-In this topic we will explain that procedure of inserting new element or number into an array at specified position given by the user .

The following program source code is implemented using three unique manner

  •  for Loop
  • while Loop
  • do..while Loop

Follow the Algorithm

Step 1: Declare four variable size, insert, i, pos and an array named arr[50].

step 2: The size of array  an array will given by the user

step 3: Input Element  of an array will be given by the user as long as i<size

step 4: Enter element  to insert into an array

step 5: Enter position or index no to insert element 

step 6: initialize i=size;            

step 7:  loop will be performed as long as  i>pos

step 8: changing the position in following way

arr[i]=arr[i-1];

step 9: decrease the value of i and go to step 5 ;

step 10: insert the new element at specified position given by user

arr[pos]= insert;

step 11: Arrange new array in following way

initialize i=0

step 12:   loop will be performed as long as  i<size+1

step13:    Display element into an array

step 14: increase value of i and go to step 12

step 9: End 

 

[/et_pb_text][et_pb_text _builder_version=”4.4.0″]

Source Code using For Loop

[/et_pb_text][et_pb_text _builder_version=”4.4.0″ text_font=”Georgia|600|||||||” text_text_color=”#000000″ border_radii=”on|10px|10px|10px|10px” border_width_all=”1px” border_color_all=”#8300e9″]
void main()
{
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);
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\n",arr[i]);
}

}

 

[/et_pb_text][et_pb_text _builder_version=”4.0.9″ text_font=”Georgia|700|||||||” text_text_color=”#000000″]OUTPUT[/et_pb_text][et_pb_image src=”https://digitalmind.online/wp-content/uploads/2020/01/insert_element.png” _builder_version=”4.0.9″][/et_pb_image][et_pb_text _builder_version=”4.4.0″]

Source Code Using While Loop

[/et_pb_text][et_pb_text _builder_version=”4.4.0″ text_font=”Verdana|600|||||||” text_text_color=”#000000″ border_radii=”on|8px|8px|8px|8px” border_width_all=”3px” border_style_all=”double”]
 void main()
{
	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);
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\n",arr[i]); } }  
[/et_pb_text][et_pb_text _builder_version=”4.4.0″]

Source Code using Do..While Loop

[/et_pb_text][et_pb_text _builder_version=”4.4.0″ text_font=”Verdana|600|||||||” text_text_color=”#000000″ border_radii=”on|8px|8px|8px|8px” border_width_all=”3px” border_style_all=”double”]
void main()
{
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);
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\n",arr[i]);
  }

}
[/et_pb_text][et_pb_text _builder_version=”4.4.2″ hover_enabled=”0″]

Recommended the following post

C program to find out the Second Highest number of Array elements.

C program to find out second-smallest number within the array

C Program to delete an element from an array using loops ( for, while and do..while).

You can visit my Facebook page

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]