Program to print Floyd's triangle star pattern in C++
Written by
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:
- Prompt the user to input the number of rows they want in the pattern.
- Store the number of rows in a variable. This will allow you to use the value in your loop.
- Use a for loop to iterate through the number of rows. The loop should run for the number of rows that the user specified.
- 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.
- Inside the inner loop, use a function like printf or cout to print a star character ("*").
- 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
*
**
***
****
*****