Reverse Pyramid of Asterisk

does any one know the code for creating this pyramid:

(3 spaces) *
(2 spaces) **
(1 space) ***
(no space) ****

it looks like a righ-aligned right-triangle pyramid.
You can probably use something you learned recently.
1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;

int main()
{
cout<<"   *\n  **\n ***\n****\n";
}
The above code is wrong, because you probably have to use a for loop. Like this:
1
2
3
4
5
#include "stdio.h"
int main(){
	const char*s="   ****";
	for(int i=0;i<4;i++)printf("%.4s\n",s+i);
}
I know i was just joking. this is how I would actually do it:
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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int a;

    for(a = 0; a < 4; a++)
    {
        if(a == 1)
            cout<<"    *\n";
        
        else if(a == 2)
            cout<<"  **\n";

        else if(a == 3)
            cout<<" ***\n";

        else if(a == 4)
            cout<<"****";

        else
            exit (1);
    }
}
Lol... azuresapphic88, rules strictly say you can't give your homework, you CAN however ask for help... So.. what part of the question didn't you understand..? Can't you observe the relation between the line number, the number of spaces in that line, and the number of * in that line?
hi caprico, i never have programming before, so i may not get the relation between spaces and line number. However, I was able to write the code for a left-aligned pyramid of asterisk. I've been trying to figure it out for days, but no conclusion came up satisfactorily. That is why i came here and asked the question.

rocketboy9000 (536): I have not learn the [#include "stdio.h"], so when I try to insert system ("pause");, it won't work. I don't know how to freeze the program !?!

Aramil of Elixia (102): Same problem, it does take system ("pause"), but it does not freeze the program. Why??
system is in "stdlib.h", include that.
Or just put getchar(); at the end.
Rocketboy9000: Thanks, I've been writing get char ();.
No wonder it won't work !!
See, try making a table of the line number (variable i), spaces before the first star (variable j) and the number of stars (variable k), so it would probably go something like this (for 5 lines):

Line no.   Spaces    Stars

1            4           1
2            3           2
3            2           3
4            1           4
5            0           5


Now can you see? The relation between stars and lines is obvious, they're equal! try observing relation between spaces and line no. now.. Remember, that the above table is for 5 lines... Keep that in mind when you figure out the pattern...
@rocketboy: shouldn't be cstdlib
Both headers will get you the same functions/types, though cstdlib will place them in the std namespace as well.
Topic archived. No new replies allowed.