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.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Share on:

NK Coderz is a Computer Science Portal. Here We’re Proving DSA, Free Courses, Leetcode Solutions, Programming Languages, Latest Tech Updates, Blog Posting Etc.

Leave a Comment