C program to delete an element from the array

Data structure’s C program to delete an element from the array. Enter the array size and then it’s elements. After this enter the location of the element you want to delete and the item will be deleted. Below is the code of the above program

/* Program in C to delete an element from the array */

#include<stdio.h>

void main()
{
     int a[20], n, loc, item,i;
     
     printf("\nEnter size of an array: ");
     scanf("%d", &n);

     printf("\nEnter elements of an array:\n");
     for(i=0; i<n; i++)
     {
              scanf("%d", &a[i]);
     }
     
     printf("\nEnter location of deletion: ");
     scanf("%d", &loc);
     
     item = a[loc-1];
     for(i=loc-1; i<n; i++)
     {
              a[i] = a[i+1];
     }
     n--;
     printf("\nITEM deleted: %d", item);
     
     printf("\n\nAfter deletion:\n");
     for(i=0; i<n; i++)
     {
              printf("\n%d", a[i]);
     }
     getch();
}

Input –
Enter array size – 5
Enter array elements – 5   2    15    50    23
Enter Location of element to be deleted – 4

Output-
Array after deletion is  – 5   2   15   23

yashan has written 69 articles

3 thoughts on “C program to delete an element from the array

  1. mohansai says:

    Search and remove the element from the given input array and then sort the remaining array
    elements, store that in to the output array. Consider input1 is array elements, input2 is array size
    and input3 is the search element.
    Eg:
    Input1= {54, 26, 78, 32, 55}
    Input2= 5
    Input3=78
    Ouput1= {26, 32, 54, 55}
    Business Rules:
    i) If any of the array element (input1) is negative store -1 into the output array.
    ii) If the array size (input2) is negative store -2 into the output array.
    iii) If the search element (input3) is not present in the array (input1) store -3 into the output array.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>