Printing a “triangle” of asterisks, in c++

2019-02-16 02:21发布

i want to make a generic code that prints such output, mean user can enter any value +whole.

    *
   * *
  * * *
 * * * *
* * * * *

for 5

and

  *
 * *
* * *

for 3.

here what i make. but it prints just right triangle. Any helps. thanks in advance.

#include <iostream>
using namespace std;

int main()
{
int val;

cout << "Enter the number: ";
cin >> val;

int t  = val;
int x = val;

for(int r = 1; r <= val; r++)
{


for(int c  = 1; c <=t ; c++)
{
    if(c < x)
    {
    cout << ' ';
    }
    else
    {
    cout << "*";
    }
}    
cout << endl;
x = x-1;
}
return 0;
}

标签: c++ geometry
1条回答
ゆ 、 Hurt°
2楼-- · 2019-02-16 02:41

place 'space' after * in cout, it adjust all your sequence. mean cout << "* "; thats it.

查看更多
登录 后发表回答