Program to print Floyd's triangle star pattern in C++

Written by

Juhi Kamdar

In this example, we’ll print Floyd’s triangle. Floyd’s triangle is a right-angled triangle. The Floyd’s triangle is as given below:

*
* *
* * *
*  * * *

Algorithm:

  1. Prompt the user to input the number of rows they want in the pattern.
  2. Store the number of rows in a variable. This will allow you to use the value in your loop.
  3. Use a for loop to iterate through the number of rows. The loop should run for the number of rows that the user specified.
  4. Inside the for loop, use another for loop to print the stars in each row. The inner loop should run for the number of rows that the user specified.
  5. Inside the inner loop, use a function like printf or cout to print a star character ("*").
  6. After the inner loop finishes executing, print a newline character to move to the next row.

Code: 

#include <iostream>
using namespace std;

int main()
{
    int n, i, c;
    cout << "Enter the number of rows: ";
    cin >> n;
    cout << "\n";
    for (i = 1; i <= n; i++)
    {
        for (c = 1; c <= i; c++)
        {
            cout << "*";
        }
        cout << "\n";
    }
    return 0;
}

Output:

Enter the number of rows: 5

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