How recursion works inside a loop?

void print(int N,int i)
{
if(i==N)
{
cout<<i<<endl;
return;
}
cout<<i<<endl;
for(j=1;j<=3;j++)
{
print(i+1,N);
}
}

int main()
{
int i=0;
Int N=3;
print(N,i);
return 0;
}

I wnat to ask how this code is giving output, i am unable to understant when function is calling insode loop.

hi @subhamkumar341768
which ques are u referring to?? also kindly save ur code on coding blocks ide and send link…

I am not reffering to any Question, i just want to ask like how recursion works inside a loop, i have given a code, with the help of that i want to understand this.

hi @subhamkumar341768
ur code will give TLE only… bcoz in every recursive call, it again goes into for loop and further in recursive call…

But this code is giving some output?

No the code u sent will got into TLE and hence give error…

#include
#include
using namespace std;
void print(int i,int N)
{
int j;
if(i==N)
{
cout<<i<<endl;
return;
}
cout<<i<<endl;
for(j=1;j<=3;j++)
{
print(i+1,N);
}
}
int main()
{
int i=1;
int N=3;
print(i,N);

return 0;

}
this code is running and giving output
1
2
3
3
3
2
3
3
3
2
3
3
3
i am not able to understand how it is printing output and what exactly is the way to print??

hi @subhamkumar341768
try to make the recursive tree on paper, it would help u better understand the logic and debug the o/p…

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.