Pyramid pattern based on NUMBER OF STARS

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

I have an idea how to do it based on numer of rows, but i dont know how to do it when I can only input number of stars.
http://www.cplusplus.com/doc/tutorial/control/
https://www.learncpp.com/cpp-tutorial/for-statements/

Outer for loop goes from i = 1 to n
Inner for loop goes from j = 0 to i

Show us an attempt of you trying it yourself.

_____

Edit: I misread the problem. Having the total number of stars as input doesn't make much sense. Show us what the output should look like for, say, n = 7.
Should it look like
*
**
***
*
?
Last edited on
Why don't you just keep writing out stars in a pyramid until you reach the requisite number of stars?

BTW, note that most numbers aren't pyramid numbers n(n+1)/2. What numbers of stars are you allowing and what do you intend with incomplete final rows?
Yeah i know that for some numbers it doesnt make sense, but if i had to take a guess:
For 7* it'd be:
*
**
***
*

etc.
I was told many times to input number of stars instead of rows. Even though it doesnt make sense for some numbers.
and they did not say what to do when it does not make sense?
simple things, do what you can (as above, stop when hit # of stars) or drop the last row (don't use all the stars asked for is ok??) or... you can do something fancy, like find the # of rows you can do correctly, figure out how many you have left over, and insert the row that holds that many a second time.
eg for 7, 6 makes 3 rows, 1 left over, so you get
*
*
**
***

or for 9, you have 3 left over:
*
**
***
***
****

or, you can add 1 to various rows to eat the left overs... (your choice where to stick the extras)
*
**
****
for 7^^ or 9:
**
***
****

You still will have a nested loop, but the inner loop will be a for loop, and the outer loop can just be a while (true) loop.

The inner for loop goes from [0, row) (row initially set to 1). Each outer loop, you increment row, and print a newline.

Also keep a "total_stars_printed" variable that you increment each time you print a star, inside each inner for loop. Also inside each inner for loop iteration, check if total_stars_printed == n. If it does, return 0; from main.

You could also do it mathematically like jonnin is saying, but a while loop works too.
Last edited on
Show the 'extras' as a separate symbol eg '-' instead of '*'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
   int stars;
   cout << "How many stars? ";   cin >> stars;
   int rows = 0.5 * ( -1 + sqrt( 1 + 8.0 * stars ) );
   int full = rows * ( rows + 1 ) / 2;
   int remainder = stars - full;
   for ( int r = 1; r <= rows; r++ ) cout << string( r, '*' ) << '\n';
   if ( remainder ) cout << string( remainder, '*' ) + string( rows + 1 - remainder, '-' ) << '\n';
}


How many stars? 10
*
**
***
****


How many stars? 12
*
**
***
****
**---
Automated/annotated along the same lines
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
28
29
30
31
32
33
#include <iostream>
#include <string>

int main()
{
    std::string output;
    int row{0};
    int remainder{0};
    
    for( int no_stars = 1; no_stars < 20; no_stars++)
    {
        output.clear();
        row = 0;
        remainder = 0;
        
        for(int i = 0; i * (i + 1)/2 <= no_stars; i++)
        {
            output += std::string(i, '*') + '\n';
            remainder = no_stars - row * (row+1)/2;
            row++;
        }
        
        if(remainder != 0)
            output += std::string(remainder, '-') + '\n';
        
        std::cout
        << "No. of stars: " << no_stars << " No. rows: " << row - 1
        << " Remainder: " << remainder
        << output << '\n';
    }
    
    return 0;
}


No. of stars: 1 No. rows: 1 Remainder: 0
*

No. of stars: 2 No. rows: 1 Remainder: 1
*
-

No. of stars: 3 No. rows: 2 Remainder: 0
*
**

No. of stars: 4 No. rows: 2 Remainder: 1
*
**
-

No. of stars: 5 No. rows: 2 Remainder: 2
*
**
--

No. of stars: 6 No. rows: 3 Remainder: 0
*
**
***

No. of stars: 7 No. rows: 3 Remainder: 1
*
**
***
-

No. of stars: 8 No. rows: 3 Remainder: 2
*
**
***
--

No. of stars: 9 No. rows: 3 Remainder: 3
*
**
***
---

No. of stars: 10 No. rows: 4 Remainder: 0
*
**
***
****

No. of stars: 11 No. rows: 4 Remainder: 1
*
**
***
****
-

No. of stars: 12 No. rows: 4 Remainder: 2
*
**
***
****
--

No. of stars: 13 No. rows: 4 Remainder: 3
*
**
***
****
---

No. of stars: 14 No. rows: 4 Remainder: 4
*
**
***
****
----

No. of stars: 15 No. rows: 5 Remainder: 0
*
**
***
****
*****

No. of stars: 16 No. rows: 5 Remainder: 1
*
**
***
****
*****
-

No. of stars: 17 No. rows: 5 Remainder: 2
*
**
***
****
*****
--

No. of stars: 18 No. rows: 5 Remainder: 3
*
**
***
****
*****
---

No. of stars: 19 No. rows: 5 Remainder: 4
*
**
***
****
*****
----

Program ended with exit code: 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
   int stars;
   cout << "How many stars? ";   cin >> stars;
   for ( int i = 1, j = 1; stars--; j++ )
   {
      cout << '*';
      if ( j == i )
      {
         j = 0;
         i++;
         cout << '\n';
      }
   }
}
Thank you guys very much! It helped me a lot.
Also is there anyway to print this pyramid reverse?
dio123321 wrote:
print this pyramid reverse?


What is that supposed to mean?
Topic archived. No new replies allowed.