DESCRIPTION: This program adds an element to an array of integers at the valid position specified by the user.
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
main()
{
int array[100], position, i, n, value;
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", &array[i]);
printf("Enter the position where you wish to insert an element\n");
scanf("%d", &position);
if(position>n+1)
{
printf("Invalid\n");
exit(0);
}
printf("Enter the value to insert\n");
scanf("%d", &value);
for (i= n-1;i>= position-1;i--)
array[i+1] = array[i];
array[position-1] = value;
printf("Resultant array is\n");
for (i= 0; i<= n; i++)
printf("%d ", array[i]);
}
OUTPUT WINDOW:
No comments:
Post a Comment