Select Page

Delete an element from an Array

C programming to delete an element from an Array-In this topic we will explain that procedure of deleting element or number from an array. Array size and Array element will be input by the user.

The following program source code is implemented using three unique manner

  •  for Loop
  • while Loop
  • do..while Loop

Follow the Algorithm

Step 1: Declare four variable size, i, del, count and an array named arr[50].

Step 2: initialize count=0;

step 3: The size of array  an array will given by the user

step 4: initialize  i=0 and  Input Element  of an array will be given by the user as long as i<size

step 5: Enter element  to be delete from  an array

step 6:  start finding element from first index i=0

step 7:  loop will be performed as long as  i<size             

step 7:  check if (arr[i]==del) true then

step 8: j=i and loop will perform  as long as j<size-1

step 9: changing the element in following way

arr[j]=arr[j+1];

step 10: increase the value of j and repeate step 8  ;

step 11: increment count and break and repeate step-6

step 12: if (count ==0)

element not found

else

element will be delete

step 13:  Arrange the new array

step 14:   i=0 and loop will be performed as long as  i<size-1

step15:    Display  array element and increase value of i

step 16: repeat step 14

step 17: End 

 

Source code using for loop

void main()
{
int arr[50], size, i, del, count=0;
printf("Enter array size : ");
scanf("%d",&size);
printf("Enter array elements :\\n") ;
for(i=0; i<size; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter element to be delete : ");
scanf("%d",&del);
for(i=0; i<size; i++)
{
if(arr[i]==del)
{
for(int j=i; j<(size-1); j++)
{
arr[j]=arr[j+1];
}
count++;
break;
}
}
if(count==0)
{
printf("Element not found..!!");
}
else
{
printf("Element deleted successfully..!!\\n");
printf("Now the new array is :\\n");
for(i=0; i<(size-1); i++)
{
printf("%d\\n",arr[i]);
}
}
}

 

OUTPUT

Source code using while loop

void main()
{
int arr[50], size, i,j, del, count=0;
printf("Enter array size : ");
scanf("%d",&size);
printf("Enter array elements :") ;
i=0;
while(i<size)
{
scanf("%d",&arr[i]);
i++;
}
printf("Enter element to be delete : ");
scanf("%d",&del);
i=0;
while( i<size)
{
if(arr[i]==del)
{
j=i;
while( j<size-1 )
{
arr[j]=arr[j+1];
j++;
}
count++;
break;
}
i++;
}
if(count==0)
{
printf("Element not found..!!");
}
else
{
printf("\nElement deleted successfully..!!");
printf("\nNow the new array is :");
i=0;
while( i<size-1 )
{
printf("%d",arr[i]);
i++;
}
}
}
OUTPUT

Source code using do..while loop

 

void main()
{
int arr[50], size, i,j, del, count=0;
printf("\n\tEnter array size : ");
scanf("%d",&size);
printf("\n\tEnter array elements :") ;
i=0;
do
{
scanf("%d",&arr[i]);
i++;
}
while( i<size);
printf("\n\tEnter element to be delete : ");
scanf("%d",&del);
i=0;
do
{
if(arr[i]==del)
{
j=i;
do
{
arr[j]=arr[j+1];
j++;
}while( j<size-1);
count++;
break;
}
i++;
}while( i<size);
if(count==0)
{
printf("\n\tElement not found..!!");
}
else
{
printf("\n\tElement deleted successfully..!!");
printf("\n\tNow the new array is :");
i=0;
do {
printf("\n\t%d\n",arr[i]);
i++;
} while(i<size-1);
}
}

 

OUTPUT