C Program to Delete a Specific Value from an Array

C program to delete a value from an array

Also Read:Union vs Structure in C Programming Language

The logic will contain two sub logic to perform the operation
1) Array Searching
2) Shifting

In array searching, we take a particular value to be searched and test it inside the loop. When the value gets matched the compiler prints the message «Found» with its position.

For shifting we run a loop up to (size of loop — 1) and shift the position of marked element to (position + 1).

C program for above logic:

#include<stdio.h> int main(){   int no[100],pos,i,c,k=0,loc;   printf("Enter the range of array");   scanf("\%d",&c);   for(i=0;i<c;i++){       printf("Enter Element \%d:",i+1);       scanf("\%d",&no[i]);   }   printf("The array is:");   for(i=0;i<c;i++){       printf("\%d",no[i]);   }   printf("Enter the element you want to delete:");   scanf("\%d",&pos);   for(i=0;i<c;i++){       if(no[i]==pos){           loc=i;           k=1;           break;       }   }   if(k==1){       for(i=loc;i<c-1;i++){           no[i]=no[i+1];       }       printf("The resultant array is:");       for(i=0;i<c-1;i++){       printf("\%d",no[i]);       }   }   else{       printf("Element not found....");   } return 0; }

Output of the above code:

Thanks for reading! Tweet your queries and feedback to @PsychoCodes ,leave a message on our Facebook page or comment below. Also, don’t forget to subscribe to our Newsletter at the top of this page. If you like the article then please share it and help us grow.


C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Macros in C with examples C Tutorials
30 January 2018

Armstrong Number program in C and C++
29 January 2018

Creating a Fixed Header Navigation Bar with on scroll effect
28 January 2018

C program to find Modular Multiplicative Inverse of two Relatively Prime Numbers
25 January 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий