Right Justified Triangle

Does anyone know how to create a right-justified triangle in C++?


Two ways come to mind.

One is to create a left-justified triangle and use cout << setw() << right to alter the way it is displayed.

Another way is to create a left-justified triangle from spaces, then pad it to a fixed width using whatever displayable character you choose.
thnk u chervil, but i don't know were would i put the setw() by the way. Would you mind making a simple code for it... so far this is the code for left justified one:

int row, col, ans;


cout<<"input rows";
cin >>ans;
cout<<endl;

for(row = 1 ; row<= ans; row++ )
{
for (col = 1; col <= row; col++)
{
cout<<"*"
}
cout<<endl;
}
See

setw
http://www.cplusplus.com/reference/iomanip/setw/

Note that setw() resets after each use, unlike most other manipulators. If you use (e.g.) setfill() or setprecision(), the value applies until you change it again.

Andy

PS As it looks like you're new here...

Please use code tags --> the <> button below the new post box or to the right of the post edit box. As well as looking nice, it provides line numbers which make it easier to refer to posted code.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {
    cout << "Hello nicely formatted world!\n";

    return 0;
}


And a couple of articles:

How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/

How to use tags
http://www.cplusplus.com/articles/z13hAqkS/
(for code and other types of tag)

You can even go back and add tags to your earlier posts...
Last edited on
Topic archived. No new replies allowed.