C Program To Delete An Element In An Array

C Program To Delete An Element In An Array: A simple program to delete an element in an array

#include <stdio.h>

void main(){
    int arr[2], i, pos;

    printf("Enter the elements in the array: \n");

    for (int i = 0; i < 2; i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("Enter the index at which you want to delete: ");
    scanf("%d",&pos);

    if(pos < 0 || pos > 2)
    {
        printf("Invalid position!");
    }
    else
    {
        for(i=pos-1; i<2; i++)
        {
            arr[i] = arr[i + 1];
        }

        printf("\nElements of array after deletion are : ");
        for(i=0; i<1; i++)
        {
            printf("%d\t", arr[i]);
        }
    }
}

Explanation: Just copy the right element to the left. That’s it by using for loop.

Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment