/* */ Click to Join Live Class with Shankar sir Call 9798158723
Q.) WAP to delete an element in an array With C program.
	#include< stdio.h>
#include< stdlib.h>
int main()
{
	int *a,n,i,element;
	printf("Enter size of array:");
	scanf("%d",&n);

	a=malloc(sizeof(int)*n);

	printf("Enter %d Elements:",n);
	for(i=0;i< n;i++)
	{
		scanf("%d",&a[i]);
	}
	printf("Enter a Element to delete:");
	scanf("%d",&element);
	for(i=0;i< n;i++)
	{
		if(a[i]==element)
		{
			while(i< n-1)	
			{
				a[i]=a[i+1];
				i++;
			}
			a[i]=0;
		}
	}
	printf("After deleting %d:",element);
	for(i=0;i< n;i++)
	{
		printf("%d ",a[i]);
	}
	return 0;
				
}
			
Output
Sample Input 1:
5 5 7 9 3 1 9
Sample Output 1:
5 7 3 1

Program Explanation


Step 1 : Include header files (#include< stdio.h> and #include < stdlib.h>).

Step 2 : Start with main function with return type.

Step 3 : parenthesis to start and end the program { }.

Step 4 : declare variables with data type i.e, '*a,n,i,element' is an integer type so we use "int" data type.

Step 5 : Use output function printf() to print the output on the screen.

Step 6 : Use input function scanf() to get input from the user.

Step 7 : here, delete an element in an array ,Enter size of array: scanf function is used to seen the size of array and then printf function was give meassage Enter Elements: after enter element for loop was check the condition for(i=0;i< n;i++), the condition was true value of n will go under the loop and scanf function was seen the a[i] , and the printf function was give a message 'Enter a Element to delete:' and scanf function was scan the 'element' , and for loop was check condition for(i=0;i< n;i++),if condition was true then go inside the loop and check statement a[i]==element with the help of 'IF statement' then a[i]==element the value was check go to while loop and check the condition condition was true , the printf give message 'After deleting ,element' , and for loop was run and give output of the programme. ,

Step 8 : using getch() function to hold the screen.