- Program to insert an element in an array at a given position
- C program to insert an element in an array without using function
Program #1: Write a c program to insert an element in array without using function
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- int insarray[100], pos, c, n, value;
- printf("Enter number of elements of array\n");
- scanf("%d", &n);
- printf("Enter all %d elements\n", n);
- for (c = 0; c < n; c++)
- scanf("%d", &insarray[c]);
- printf("Enter the specific position where you wish to insert an element in array\n");
- scanf("%d", &pos);
- printf("Enter the value to insert\n");
- scanf("%d", &value);
- for (c = n - 1; c >= pos - 1; c--)
- insarray[c+1] = insarray[c];
- insarray[pos-1] = value;
- printf("Resultant array is\n");
- for (c = 0; c <= n; c++)
- printf("%d\n", insarray[c]);
- getch();
- }
No comments