Write a program to delete an element of an array in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n)Space Complexity: O (1)Open Source: MIT
1
2
    
3
    #include <stdio.h>
4
    #include <conio.h>
5
    int main()
6
    {
7
        int a[20], num, pos, i;
8
        // clrscr();
9
        printf("Enter the total number of the elements of array: ");
10
        scanf("%d", &num);
11
        for (i = 0; i < num; i++) /* scaning the elemtents of the array */
12
        {
13
            printf("Enter  %d element of array: ", i + 1);
14
            scanf("%d", &a[i]);
15
        }
16
        printf("\nEnter the position of the element to delete: ");
17
        scanf("%d", &pos);
18
        for (i = pos; i < num; i++) /* overflow of the  elements */
19
            a[i - 1] = a[i];        /* delete the position of the element*/
20
        a[num - 1] = 0;             /* assign the 0 value to the last position*/
21
        printf("\nAfter deletion %d position of the element the array is", i);
22
        for (i = 0; i < num - 1; i++)
23
            printf("\nElement is %d at %d position", a[i], i + 1);
24
      
25
        return 0;
26
    }
27
28
29
        

Output

1
2
    Enter the total number of the elements of array: 5
3
    Enter  1 element of array: 2
4
    Enter  2 element of array: 4
5
    Enter  3 element of array: 3
6
    Enter  4 element of array: 5
7
    Enter  5 element of array: 9
8
    
9
    Enter the position of the element to delete: 3
10
    
11
    After deletion 5 position of the element the array is
12
    Element is 2 at 1 position
13
    Element is 4 at 2 position
14
    Element is 5 at 3 position
15
    Element is 9 at 4 position
16
     

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run delete an element of an array program in C-Language?

To run delete an element of an array program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More about deleting an element program in C language

Aim: To delete an element from an array at the user provided input index.

Concepts used in this program

  • Array: To store the same type of data as 1-D (one dimension).
  • For loop: To traverse and print every element of an array.
  • Element Shifting: To fill the deleted element's position, all the next elements are shifted by -1 index.

Real-life use cases

  • To remove any record from a to-do list app.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.