C Program to Delete Element from Array

#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int array[50], size, i, element, count=0;
    printf("Enter array size : ");
    scanf("%d",&size);
    printf("Enter array elements : ");
    for(i=0; i<size; i++)
    {
        scanf("%d",&array[i]);
    }
    printf("Enter element to be delete : ");
    scanf("%d",&element);
    for(i=0; i<size; i++)
    {
        if(array[i]==element)
        {
            for(int j=i; j<(size-1); j++)
            {
                array[j]=array[j+1];
            }
            count++;
            break;
        }
    }
    if(count==0)
    {
        printf("Element not found..!!");
    }
    else
    {
        printf("Element deleted successfully..!!\n");
        printf("Now the new array is :\n");
        for(i=0; i<(size-1); i++)
        {
            printf("%d ",array[i]);
        }
    }
    getch();
}

Run1:

Run2:

No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...