Delete an Element from an Array in C
Here, you will get example code to delete an element from an array in c programming. In this program we will get input an element number and then delete that element from the array list. We will be implementing this program on 1d array in C.
Follow the following steps to complete the program:-
Step 1 Create an array list with input of some numbers.
Step 2 Input location of element, to be deleted.
Step 3 Start a loop till location to be found, delete the element, and continue loop till end and shift next element to current one.
Step 4 Print the array list.
Program to Delete an Element from an Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<stdio.h> int main() { int arr[10], k,n,i; printf("\n Enter the size of the array : "); scanf("%d", &n); printf("\n Enter Array elements : "); for(i=0;i<=n-1;i++) { scanf("%d", &arr[i]); } printf("\nEnter the location of the element to delete : "); scanf("%d", &k); for(i=k-1;i<=n-1;i++) { arr[i]=arr[i+1]; } printf("\n *****Array elements after delete elements in list*****\n"); for(i=0;i<=(n-2);i++) {printf("%d ", arr[i]);} getch(); return 0; } |
Output
Enter the size of the array : 7
Enter Array elements : 1 2 3 4 5 6 7
Enter the location of the element to delete : 5
*****Array elements after delete elements in list*****
1 2 3 4 6 7
Check out our other C programming Examples