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 an item: 6
A new list is: 1 2 3 4 5
Example: How to delete an element in an array in c
#include<stdio.h>
int main() {
int arr[10], i, n, num, p = 0;
printf("Enter size of an array:");
scanf("%d", &n);
printf("Enter array elements:");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter 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("New array list:");
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:30
New array list:10 20 40 50