Triangle recursive function

Can anybody help me solve this problem??Thank You.

Write a complete C++ program which contains a recursive function to print a triangle filled with character.

Write the prototype of prtTriangle as
void prtTriangle (int n, char ch);
By calling prtTriangle(9,'@') from the main function, the output is printed as follows:
@
@@
@@@
@@@@
@@@@@
@@@@@@
@@@@@@@
@@@@@@@@
@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@
@@@@@@@@
@@@@@@@
@@@@@@
@@@@@
@@@@
@@@
@@
@
We don't do homework for free.
Hint:

Try running these two recursive programs and see what output you get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Program 1:
#include <iostream>

void print_x( int x ) {
    if( x >= 0 ) {
        std::cout << x << std::endl;
        print_x( x - 1 );
    }
}

int main() {
    print_x( 9 );
}

// Program 2:
#include <iostream>

void print_x( int x ) {
    if( x >= 0 ) {
        print_x( x - 1 );
        std::cout << x << std::endl;
    }
}

int main() {
    print_x( 9 );
}


Note: all I did was reverse the order of two lines of code.

(of course you'll need to put these two programs in two different .cpp files).
i hav done tis...
but still wrong~~
Can help me check this???

#include<iostream.h>
#include<conio.h>
#include <string>
using namespace std;

void prttriangle(int n, char ch)
{
for (int i = 0; i > n; i++)
cout << ch;
cout << endl;
if (n > ch)
prttriangle(n + 1, ch);
for (int i = 1; i > n; i++)
cout << ch;
cout << endl;
}


int main()
{
int x;
char y=1;

cout<<"key in your value,n"<<endl;
cin>>x;
cout<<"key in your character, ch"<<endl;
cin>>y;

prttriangle(x,y);
getch();

}
Use code tags please.

There should not be any loops in your function. The function should call itself to basically simulate a loop.
Did you try to run the programs I wrote above? One program uses head recursion, the other tail
recursion. Do you not want something very similar to the combination of those programs?
Topic archived. No new replies allowed.