C Program to delete an element in an array


In this program, You will learn how to delete an element in an array in c.


List is: 1 2 3 4 5 6

Delete item: 6

New list is: 1 2 3 4 5


Example: How to delete an element in an array in c

Run it Now
#include<stdio.h>

int main() {

   int arr[10], i, n, num, p = 0;

   printf("Enter Size of an Array :");
   scanf("%d", &n);

   printf("\nEnter Array Elements :");
   for (i = 0; i < n; i++) {
       scanf("%d", &arr[i]);
   }
   printf("\nEnter Element for Delete :");
   scanf("%d", &num);

   for (i = 0; i < n; i++) {
       if (arr[i] == num) {
           arr[i] = arr[i + 1];
           p = 1;
       } else if (p > 0) {
           arr[i] = arr[i + 1];
       }
   }

   printf("\nNew Array List is :");
   for (i = 0; i < n - p; i++) {
       printf("%d ", arr[i]);
   }

   return 0;
}

Output:

Enter Size of an Array :5
Enter Array Elements :10 20 30 40 50
Enter Element for Delete :20
New Array List is :10 30 40 50