Notification texts go here Contact Us Click here

Program to Insert an Element at Specific Position in Array in C Language.

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

       ➤    Insert an Element at Specific Position in Array in C Language.

1In this problem First, we have to take an array and declare the size of
   an array and, as usual, Insert the elements in an array. 
   Till now our array is storing the elements only, this is a simple Array program in C.
2The next step is to ask the user to Insert an Element which user's wants
   to store in a Specific Position of an array. Ask for the the position where 
   an element wants to store. So basically here we have asked the user to
   New Element and a Specific Position of an Array.
 
3. Here we have to increase the length of an Array by 1 and shift the Array element by 1.
   There will be no issue with the size of the array cause we already increase 
   the size of the array by 1. We only have to traverse to the Specific Position 
   and store the new Item and shift the existing Elements. 

  1.  Program to Insert an Element at Specific Position in Array in C        Language.

  1.  
  2. #include <stdio.h>
  3.  
  4.  int main()
  5.  {
  6.  
  7.   int i, arr[50], pos, len;
  8.   int newitem;
  9.      //user's initialize size of array
  10.   printf("Enter the Size of the Array:\n"); 
  11.   scanf("%d", &len);
  12.     //Enter all the initialize elements
  13.   printf("Enter %d elements for the array:\n", len); 
  14.   for (= 0; i < len; i++)
  15.   {
  16.   scanf("%d", &arr[i]);
  17.   }
  18.      //Enter the new elements to be insert
  19.   printf("Enter the new element in an Array:\n");  
  20.   scanf("%d", &newitem);
  21.     // Enter the specific position where elements to be insert in array 
  22.   printf("Enter the Specific Position in Array:\n");
  23.   scanf("%d", &pos);
  24.  
  25.   len++;
  26.   pos--;
  27.   i = len - 1;
  28.  
  29.   while (>= pos)
  30.   {
  31.   arr[i] = arr[- 1];
  32.   i--;
  33.   }
  34.  
  35.   arr[pos] = newitem;
  36.   printf("Array after inserting new element\n");
  37.  
  38.   for (= 0; i < len; i++)
  39.   {
  40.   printf(" %d", arr[i]);
  41.   }
  42.  
  43.   return 0;
  44.  }
  45.  
  46.  OUTPUT :
  47.  
  48. Enter the Size of the Array:
  49. 4
  50. Enter 4 elements for the array:
  51. 23
  52. 45
  53. 67
  54. 78
  55. Enter the new element in an Array:
  56. 89
  57. Enter the Specific Position in Array:
  58. 3
  59. Array after inserting new element
  60.  23 45 89 67 78

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.