Please explain how to approach this problem

I have tried several times but didn’t get the logic

You can simply run two nested loops, from 1 upto n and then check
if(i==j || j==1) cout<<i;
else cout<<0;