C Program to Delete an Element in an Array

In this tutorial, i am going to show you how to delete elements in an array in c programs.

C Program to Delete an Element in an Array

#include <stdio.h>
 
int main()
{
	int Array[10], Position, i, Size;
	
	printf("\n Please Enter Number of elements in an array :- ");
	scanf("%d", &Size);
	
	printf("\n Please Enter %d elements of an Array :- ", Size);
	for (i = 0; i < Size; i++)
	{
    	scanf("%d", &Array[i]);
   	}     
 
  	printf("\n Please Enter a Valid Index Position of a Element that you want to Delete :- ");
  	scanf("%d", &Position);
  	
	if(Position < 0 || Position >= Size)
  	{
  		printf("\n Please Enter a Valid Index Position between 0 and %d", Size-1);
  	}
  	else
  	{
  		for (i = Position; i < Size; i++)
   		{
	    	Array[i] = Array[i + 1];
   		}
   		Size--;
	}
 	printf("\n Final Array after Deleteing an Array Elemnt is :- ");
 	for (i = 0; i < Size; i++)
  	{
 		printf("%d\t", Array[i]);
  	}	     
 	return 0;
}

The result of the above c program; as follows:

Please Enter Number of elements in an array :- 5
Please Enter 5 elements of an Array :- 1 2 3 4 5
Please Enter a Valid Index Position of a Element that you want to Delete :- 2
Final Array after Deleteing an Array Elemnt is :- 1	2	4	5

Be First to Comment

Leave a Reply

Your email address will not be published.