Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

Write a C program to insert an element in an array

Program to insert an element in an array

#include <stdio.h>

int main()
{
   int arr[100], pos, i, n, ele;

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

   printf("Enter %d elements\n", n);

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

   printf("Enter the location  to insert an element\n");
   scanf("%d", &pos);

   printf("Enter the value to insert\n");
   scanf("%d", &ele);

   for (i = n - 1; i >= pos - 1; i--)
      array[i+1] = arr[i];

   arr[pos-1] = ele;

   printf("Result array is\n");

   for (i = 0; i <= n; i++)
      printf("%d\n", arr[c]);

   return 0;
}
Output:-


No comments:

Post a Comment