Program to insert an element in an array in C

#include <stdio.h>

int main()
{
   int array[100], position, c, n, value;

   printf("Enter number of elements in arrayn");
   scanf("%d", &n);

   printf("Enter %d elementsn", n);

   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to insert an elementn");
   scanf("%d", &position);

   printf("Enter the value to insertn");
   scanf("%d", &value);

   for (c = n - 1; c >= position - 1; c--)
      array[c+1] = array[c];

   array[position-1] = value;

   printf("Resultant array isn");

   for (c = 0; c <= n; c++)
      printf("%dn", array[c]);

   return 0;
}

Output
Enter number of elements in array
6
Enter 6 elements
9
8
6
5
4
7
Enter the location where you wish to insert an element
1
Enter the value to insert
2
Resultant array is
2
8
6
5
4
7




Discover more from PlusDigit

Subscribe to get the latest posts sent to your email.

Discover more from PlusDigit

Subscribe now to keep reading and get access to the full archive.

Continue reading

AdBlock Detected!

Please disable your AdBlocker to access this website.

Once disabled, this message will disappear automatically.