I am making a program that outputs * in the form of a triangle. I need it to do the triangle right way up and then downwards. For example if it is 5 it looks like this
*
**
***
****
*****
*****
****
***
**
*
My program is as follows but I can't seem to get it to print out like that
#include <stdio.h>
#include <iostream>
using namespace std;
void printStars(int n)
{
if (n == 1)
{
cout << "*" << endl;
}
else
{
cout << "*" << endl;
printStars(n -1);
}
}
int main (void)
{
{
printStars(5);
#include <stdio.h>
#include <iostream>
usingnamespace std;
void printStars(int n, int &target)
{
// print n stars in a line
for(int i=0; i<n; i++)
{
cout << "*";
}
cout << endl;
// add 1 to n
n++;
if(n <= target)
{
// call function recursiv
printStars(n, target);
}
else
{
// if you don't need a free line erase this else-block
cout << endl;
}
// print n stars in a line for the second part
for(int i=0; i<n; i++)
{
cout << "*";
}
cout << endl;
}
int main (void)
{
// print up to 5 stars starting with 1 star
printStars(1,5);
return 0;
}