ques : https://hack.codingblocks.com/app/practice/1/487/problem
i am not able to do this ques using recursion…
and why should we use recursion when we can do with simpler way??
ques : https://hack.codingblocks.com/app/practice/1/487/problem
i am not able to do this ques using recursion…
and why should we use recursion when we can do with simpler way??
@chaman9 this question can be done quite easily iteratively. But if you wish to practice recursion more then you should definitely try. Every recursion problem can be divided into 3 parts
In this case,
the first call will be made to f(n)
but we need to print stars in ascending order, this means that before we do anything, we need to make a call to f(n-1)
. The minimum number of stars we can print in a row is 1
, so we should stop after that, hence our base case will be if(n <= 0) return;
Now, what we actually need to do for each n
is print “n stars” in a newline. That can easily be done with the help of a for loop.
So in the end your function will look something like this
void f(n) {
//BASE CASE
if (n < = 0)
..return;
//REECURSIVE CALL
f(n - 1);
//DO SMT
for(i = 0 to I < n) {
cout << "*";
}
cout <<"\n";
}
i am able to solve it
Thanks a lot!!
Yeaah i already did that