How do I write a recursive function in C++ to display a triangle of * like this using a parameter size (e.g. 4 in the following example):
*
**
***
****
I am able to write a recursive function to display an inverted triangle like this:
****
***
**
*
The code for the inverted triangle function is:
void inverted(int a)
{
if (a==1)
{
cout<<"*"<<endl;
}
else
{
for (int i=0; i<a; i++)
{
cout<<"*";
}
cout<<endl;
inverted(a-1);
}
}
I am allowed to use a loop (e.g. for loop) to print a row of * but I must use a recursive function to control the number of rows.
Please, provide some insight into the problem. I need it to work urgently but I am not able to figure out a solution.