Introduction
In this post, I am going to write a c program to insert and delete an element in an array using a switch case. But before that, I will explain two separate c programs for inserting an element and deleting an element in an array. If you understood this program well, then our main program will be very much easy for you.
So, let’s begin.
Also read: Arrays in C for Complete Beginners
C Program to Insert an Element in an Array
#include <stdio.h> #include <stdlib.h> int main() { int a[100]; int element,i,loc,size; printf("Enter the size of an array\n"); scanf("%d",&size); printf("Enter %d array elements\n",size); for(i=0;i<size;i++) { scanf("%d",&a[i]); } printf("List before Insertion: "); for(i=0;i<size;i++) { printf("%d ",a[i]); } printf("\nEnter an element to insert\n"); scanf("%d",&element); printf("Enter a position to insert an element %d\n",element); scanf("%d",&loc); loc--; for(i=size-1;i>=loc;i--) { a[i+1]=a[i]; } a[loc]=element; printf("\nList after Insertion: "); for(i=0;i<size+1;i++) { printf("%d ",a[i]); } return 0; }
Output
Enter the size of an array
5
Enter 5 array elements
6 7 41 23 1
List before Insertion: 6 7 41 23 1
Enter an element to insert
10
Enter an position to insert an element 10
3
List after Insertion: 6 7 10 41 23 1
As you can see the above output, we have to perform the following steps to understand the logic of this program.
- Ask the user to enter the size of an array. Here, we can display our elements, but I thought it is better to ask the user to enter the size of an array.
- After knowing the size of an array, again ask the user to enter the array elements.
- In this step, we will ask the user to enter the position and element for insertion.
Also read: C Program to Find the Sum of Cubes of Elements in an Array
If we know the location or position to enter an element, then we will decrease it by 1. Why? Because, the user considers the list is starting from 1, but as c programmer, we know, the first element in the array starts from 0 and the last element is less than the total size of an array.
After knowing the location for inserting an element, we will make that location available in the array. So, we will shift each and every element to the next position one by one from that position or location. See the following code.
for(i=size-1;i>=loc;i--) { a[i+1]=a[i]; } a[loc]=element;
This for loop is shifting the elements towards the end by one. In the above output, we have read the value of size = 5, element = 4 and position = 2 (actually, we make it 2 intentionally). That means we are inserting an element 3 at position 4 in the array of size 5.
In the for loop, the value of i becomes 4 i.e. size-1. We will execute the for loop until the value of i is greater than or equal to loc i.e. 3 and the value of i will be decremented by 1.
In the body of for loop, a[i+1] = a[i] means a[5]=a[4]. And the in the next run, a[4]=a[3] and at the last a[3]=a[2]; See the following diagram.

I hope, from the above diagram, you have understood what happens when we execute the above for loop code. This program will remain the same when we write a program to insert and delete element in array in c using switch case.
C Program to Delete an Element in an Array
#include <stdio.h> #include <stdlib.h> int main() { int a[100],size,n,i,j; printf("Enter the size of an array\n"); scanf("%d",&size); printf("Enter elements\n"); for(i=0;i<size;i++) { scanf("%d",&a[i]); } printf("List before deletion\n"); for(i=0;i<size;i++) { printf("%d ",a[i]); } printf("\nEnter an element to delete\n"); scanf("%d",&n); for(i=0;i<size;i++) { if(a[i]==n) { for(j=i;j<(size-1);j++) { a[j]=a[j+1]; } break; } } printf("List after deletion\n"); for(i=0;i<(size-1);i++) { printf("%d ",a[i]); } return 0; }
Output
Enter the size of an array
5
Enter elements
1 2 3 4 5
List before deletion
1 2 3 4 5
Enter an element to delete
3
List after deletion
1 2 4 5
As you can see the above output. We are following the same steps as the insertion program which is given at the beginning. The only difference is, we are not reading the location. We are deleting an element. So, we will compare each and every element with the element we have read for deleting.
Also read: Switch Case in C Program to Calculate Area of Circle and Triangle
But, this time, after deleting an element, our list will become less than 1 of a given size. See the following diagram.

C Program to Insert and Delete an Element in an Array using switch case
#include <stdio.h> #include <stdlib.h> int main() { int a[100]; int element,i,loc,size,n,j,choice; printf("C Program to Insert and Delete an Element in an Array using switch case\n"); printf("1. Inserting an Element in an Array\n"); printf("2. Deleting an Element in an Array\n"); printf("Select your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the size of an array\n"); scanf("%d",&size); printf("Enter %d array elements\n",size); for(i=0;i<size;i++) { scanf("%d",&a[i]); } printf("List before Insertion: "); for(i=0;i<size;i++) { printf("%d ",a[i]); } printf("\nEnter an element to insert\n"); scanf("%d",&element); printf("Enter a position to insert an element %d\n",element); scanf("%d",&loc); loc--; for(i=size-1;i>=loc;i--) { a[i+1]=a[i]; } a[loc]=element; printf("List after Insertion: "); for(i=0;i<size+1;i++) { printf("%d ",a[i]); } break; case 2: printf("Enter the size of an array\n"); scanf("%d",&size); printf("Enter elements\n"); for(i=0;i<size;i++) { scanf("%d",&a[i]); } printf("List before deletion\n"); for(i=0;i<size;i++) { printf("%d ",a[i]); } printf("\nEnter an element to delete\n"); scanf("%d",&n); for(i=0;i<size;i++) { if(a[i]==n) { for(j=i;j<(size-1);j++) { a[j]=a[j+1]; } break; } } printf("List after deletion\n"); for(i=0;i<(size-1);i++) { printf("%d ",a[i]); } break; default: printf("Wrong choice, Please try again later"); } return 0; }
Output
Output 1:
C Program to Insert and Delete an Element in an Array using switch case
1. Inserting an Element in an Array
2. Deleting an Element in an Array
Select your choice : 1
Enter the size of an array
5
Enter 5 array elements
1 2 3 4 5
List before Insertion: 1 2 3 4 5
Enter an element to insert
10
Enter a position to insert an element 10
4
List after Insertion: 1 2 3 10 4 5
Output 2
C Program to Insert and Delete an Element in an Array using switch case
1. Inserting an Element in an Array
2. Deleting an Element in an Array
Select your choice : 2
Enter the size of an array
5
Enter elements
1 2 3 4 5
List before deletion
1 2 3 4 5
Enter an element to delete
3
List after deletion
1 2 4 5
Output 3
C Program to Insert and Delete an Element in an Array using switch case
1. Inserting an Element in an Array
2. Deleting an Element in an Array
Select your choice : 5
Wrong choice, Please try again later
Thank you for reading.
Also read: Switch Case in C Programming