Program to delete an element from an array

2 years ago Admin 1

Delete an element from an array

This article illustrates how to delete an element from an array in c/c++.

delete an element from an array

Here is the source code and output of both the programs.

C Programming language

Program


#include<stdio.h>
#include<conio.h>
void main()
{
int arr[100],num,pos,i;
clrscr();
printf("Enter number of elements in array:-  ");
scanf("%d",&num);
printf("Enter elements in array:- \n");
for(i=0;i<num;i++)
{
 scanf("%d",&arr[i]);
}
printf("Enter position to delete an element:- ");
scanf("%d",&pos);

if(pos<num+1)
{
     for(i=pos;i<num-1;i++)
      {
       arr[i]=arr[i+1];
      }

printf("Array after deletion is:- \n");
for(i=0;i<num-1;i++)
 {
  printf("%d\n",arr[i]);
 }
}
else
{
 printf("Sorry deletion is not possible :)");
}

getch();
}

After successfully compiling and executing the above C program,output is:-

delete an element from an array


C++ Programming language

Program


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[100],num,pos,i;
cout<<"Enter number of elements in array:-  ";
cin>>num;
cout<<"Enter elements in array:- \n";
for(i=0;i<num;i++)
{
 cin>>arr[i];
}
cout<<"Enter position to delete an element:- ";
cin>>pos;

if(pos<num+1)
{
     for(i=pos;i<num-1;i++)
      {
       arr[i]=arr[i+1];
      }

cout<<"Array after deletion is:- \n";
for(i=0;i<num-1;i++)
 {
  cout<<arr[i]<<endl;
 }
}
else
{
 cout<<"Sorry deletion is not possible :)";
}

getch();
}

After successfully compiling and executing the above C++ program,output is:-

delete an element from an array


Comment below if you are facing any problem or you find any error in the above program and also subscribe to our newsletter for more updates.


Read more c/c++ Programs published by snapcout:-

Program to sort an array in ascending order
Program to calculate the sum and average of n number
Program to find all prime numbers from array
Program to insert an element in an array
Program to find the largest element in an array


About article

Written By:- Admin

Published on:- 20/11/2017

Published By:- Snapcout


Don't forget to share the article if you like it:-