Answer is Yes and No. Ultimately, there's nothing recursion can compute that looping can't, but looping takes a lot more plumbing. Therefore, the one thing recursion can do that loops can't is make some tasks super easy.
Recursion Build Trees, Take walking a tree. Walking a tree with recursion is stupid-easy. It's the most natural thing in the world. Walking a tree with loops is a lot less straightforward. You have to maintain a stack or some other data structure to keep track of what you've done.
Often, the recursive solution to a problem is easier. That's a technical term, and it matters.
Finally, doing loops instead of recursion means to manually handle the stack. 😇
It Seems like recursion is a clear winner.😎
But There are some situation , where recursion will Fail and Loops Win's.😇 Following is the situation.
However, you may run into problems if you make a very large number of recursive calls.
For instance, let's say that the size of the stack is limited to 2 MB for your program. In this case, it can't sustain one million calls of a function like this:
int fun(int n){
if(n==0)return n;
else
return fun(n-1) + 1;
}
Here, every level of recursion uses at least 4 bytes of memory and you will run into stack overflow if you call this function with n=1123456.
Typically, some situation iterative (looping) solution will need just as much storage, but will need to manage it explicitly.
Finally the conclusion Recursion and Looping both are tool to solve a problem, but when we think the best to use, then based on the situation which suits best , which makes the algo easier or best fitted for the situation.
That's All Folks , Happy Learning 😎