Why after n>7 output is not coming proper

#include
using namespace std;
int main() {
int *ptr= new int(30);
cout<<*ptr<<endl;
int *ptr2=new int ;
cout<<*ptr2<<endl;
int n;
cin>>n;

int *ptr3= new int[n];


for(int i=0;i<n;i++)
{
    ptr[i]=i;
}
for(int i=0;i<n;i++)
{
    cout<<ptr[i]<<" ";
}

}

Hi bhavit
You should use ptr3 instead of ptr in code highligheted.

Hope it helps:)

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.