Tuesday, January 17, 2023

C Programming. Delete an element at desired position from an array.

Delete an element at desired position from an array.


#include<stdio.h>
int main() {
    int a[10];
    int n, i, j, x;
    printf("Enter the number of elements : ");
    scanf("%d",&n);
    printf("Enter the elements : ");
    for (i=0; i<n; i++){
        scanf("%d",&a[i]);
    }
    printf("Enter the desired position where you want to delete element : ");
    scanf("%d",&x);

    for (i=x; i<n-1; i++){
        a[i]=a[i+1];
    }
    for (i=0; i<n-1; i++){
        printf("%d ",a[i]);
    }
    return 0;
   
}

Output:-

Enter the number of elements : 5 Enter the elements : 12 58 74 32 15 Enter the desired position where you want to delete element : 3 12 58 74 15

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...