Kat H.

asked • 04/19/21

Triangle Printer. The existing code reads in a number and prints a column of stars that many rows long. Modify it so that it prints a triangle that looks like the one shown below.

The existing code reads in a number and prints a column of stars that many rows long. Modify it so that it prints a triangle that looks like the one shown below.


SizeShape


1

*

3

*
***
*****

5

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


Hint - you can either:

  1. In each row, calculate the number of stars based on the row number.
  2. Have a variable like numStars that lasts through all of the rows. Use it to keep count of how many stars there should be in the current row.

Code:


#include <iostream>

using namespace std;


int main()

{

int size;

cin >> size;


for(int row = 1; row <= size; row++) {

cout << "*";


cout << endl;

}

}

1 Expert Answer

By:

Patrick B. answered • 04/20/21

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.