Feb 6, 2020

C program to delete the element from array

Ex: Write a C program to delete the element from array. How to write a C program to delete the element from array. C program to delete the element from array.


Input from user:
Enter how many elements you want: 7
Enter the elements: 
1
2
3
4
2
5
6
Entered elements are: 
1 2 3 4 2 5 6 
Enter which element you want to delete: 2
Enter the position: 5

Expected ouput:
After deleting, array element's are:
1 2 3 4 5 6 




Program:

#include<stdio.h>
void main()
{
 int a[100],i,no,key,temp,pos;

 //Accept limit from user..
 printf("Enter how many elements you want:\n");
 scanf("%d",&key);

 //Accepting elements 
 printf("\nEnter the elements:\n");
 for(i=0;i<key;i++)
 scanf("%d",&a[i]);

 /*Print entered elements*/
 printf("Entered elements are:\n");
 for(i=0;i<key;i++)
 printf("%d ",a[i]);

 /*Accept element which want to delet*/
 printf("\nEnter which element you want to delet:\n");
 scanf("%d",&no);

 /*Accept position of element*/
 printf("Enter the position:\n");
 scanf("%d",&pos);

 /*logic to delet element*/
 for(i=pos-1;i<=key;i++)
 {
  a[i]=a[i+1];
  
 }
 /*print elements after deleting*/
 printf("After deleting, array elements are:\n");
 for(i=0;i<key-1;i++)
 printf("%d ",a[i]);

}


Above Program shows the following output:



    



No comments:

Post a Comment

If you have any doubts, please discuss here...