Remove an element from the given array using C
Hey guys once again welcome to the Decoders blog, I hope you all are doing well and today I'm present with another logic but this time with something different it is a C logic which will help us to remove a particular element from the given array using C language.
Here's the code which you can go through...........
#include<stdio.h>
int main()
{
int array[50]; //Creates an array with length 50
int n,pos,i,num;
printf("Enter the number of entries:");
scanf("%d",&num);
//Storing the values
printf("Entre the value for the array now:");
for(i=0;i<num;i++)
{
scanf("%d",&array[i]);
}
//Displaying the values
for(i=0;i<num;i++)
{
printf("The value of the array[%d] is %d\n",i,array[i]);
}
//Removing one element from the given array using the indexing method
printf("Entre the position from which you want to remove the element:");
scanf("%d",&pos);
for(i=pos-1;i<num-1;i++)
{
array[i] = array[i+1];
}
//Displaying the modified array
for(i=0; i<num-1;i++)
{
printf("The value of the array[%d] is %d\n",i,array[i] );
}
return(0);
}
Comments
Post a Comment