C Program to Delete Array Element From Given Position

Question: Write a program in C to read an array containing n elements. Also read position and then delete array element from given position.

Removing Array Element From Given Position - C Program


#include<stdio.h>

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

 printf("Enter n:\n");
 scanf("%d", &n);
 
 /* Reading array */
 printf("Enter numbers:\n");
 for(i=0;i< n;i++)
 {
  printf("a[%d]=",i);
  scanf("%d", &a[i]);
 }
 
 /* Displaying original array */
 printf("Given array is:\n");
 for(i=0;i< n;i++)
 {
  printf("%d\t",a[i]);
 }
 
 /* Reading number and position */
 printf("\nEnter position of deletion:\n");
 scanf("%d", &pos);
 
 /* Deletion */
 for(i=pos-1;i< n-1;i++)
 {
  a[i] = a[i+1];
 }
 a[n-1]=0;
 
 /* Displaying final array */
 printf("Array after deletion is:\n");
 for(i=0;i< n-1;i++)
 {
  printf("%d\t",a[i]);
 }
 
 return 0;
}