C program to delete an element from the specific location from Array

C program to delete an element from the specific location from Array:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

main()
{
   int arr[30],num,i,loc;

   printf("Enter no of elements :");
   scanf("%d",&num);

   //Reading elements in array
   printf("\nEnter %d elements :",num);
   for(i=0;i<num;i++)
    {
      scanf("%d",&arr[i]);
    }

   //Reading the location to be deleted
   printf("\nEnter location of the element to be deleted :");
   scanf("%d",&loc);

   //Deleting element
   while(loc < num)
    {
      arr[loc - 1]=arr[loc];
      loc++;
    }
    num--;

   //Printing Array
   printf("\nNew array after deleting is :");
   for(i=0;i<num;i++)
      printf("\t%d",arr[i]);
}

OUTPUT:

Enter no of elements :5

Enter 5 elements :7 5 9 3 4

Enter location of the element to be deleted :3
New array after deleting is :   7       5       3       4

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.