C++ Program to Delete an Element From an Array

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

//Program for remove element from array;
#include<iostream>
#define clrscr() system("cls")
using namespace std;
int main()
{
    int a[50],n,i,idx;
    clrscr();
    input:cout<<"\nEnter the limit: ";
    cin>>n;
    if(n>50)
    {
        cout<<"\n Invalid limit pls enter again";
        goto input;
    }
    else
    {
        cout<<"\n Enter elements in array";
        for(i=0;i<n;i++)
        cin>>a[i];

       cout<<"\n Elements of array\n";
        for(i=0;i<n;i++)
       { 
           cout<<"a["<<i<<"]:";
           cout<<a[i]<<"  ";
       } 

        cout<<"\n Enter index number for delete element";
        cin>>idx;
        if(idx>n)
            cout<<"\nInvalid index number deletion not possible";
        else
        {
              for(i=idx;i<n-1;i++)
              {
                  a[i]=a[i+1];
              }
            cout<<"\n elements of array after deletion\n";
        for(i=0;i<n-1;i++)
       { 
           cout<<"a["<<i<<"]:";
           cout<<a[i]<<"  ";
       } 


        }
        

    }



}

 

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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