Floyd's Triangle is a triangular pattern of consecutive natural numbers, where each row contains one more number than the previous row.
- The number of elements in the ith row is i.
- A counter variable is incremented after printing each number to maintain the sequence.
Illustration
Input: row = 5
Output:
1Â
2 3Â
4 5 6Â
7 8 9 10Â
11 12 13 14 15ÂInput: row = 4
Output:
1Â
2 3Â
4 5 6Â
7 8 9 10Â
There are 3 ways to print Floyd's pattern triangle pyramid
The following approaches use loops and recursion to generate Floyd's Triangle with consecutive natural numbers.
Using for Loop
The outer loop controls the rows, while the inner loop prints i numbers in the ith row. The count variable is incremented after each number is printed.
#include <bits/stdc++.h>
using namespace std;
void print_patt(int row)
{
// Initializing count to 1.
int count = 1;
// The outer loop maintains
// the number of rows.
for (int i = 1; i <= row; i++)
{
// The inner loop maintains the
// number of column.
for (int j = 1; j <= i; j++)
{
// To print the numbers
cout << count << " ";
// To keep increasing the count
// of numbers
count += 1;
}
// To proceed to next line.
cout << "\n";
}
}
// Driver Code
int main()
{
int row = 5;
print_patt(row);
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Using while Loop
The outer while loop controls the rows, while the inner while loop prints the required number of elements in each row. The count variable maintains the sequence of consecutive numbers.
#include <bits/stdc++.h>
using namespace std;
void print_patt(int row)
{
// Initializing count to 1.
int count = 1;
// Loop control variables.
int i = 1, j;
// The outer loop maintains
// the number of rows.
while (i <= row)
{
// Reverting the value of j to 1
// after each iteration.
j = 1;
// The inner loop maintains the
// number of column.
while (j <= i)
{
// To print the numbers
cout << count << " ";
// To keep increasing the
// count of numbers
count += 1;
// Increasing the count
// of inner loop.
j += 1;
}
// Increasing the count of outer loop.
i += 1;
// To proceed to next line.
cout << "\n";
}
}
// Driver Code
int main()
{
int row = 5;
print_patt(row);
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Using Recursion
The recursive approach prints one row at a time. After printing the current row, the function calls itself for the next row until all rows are printed.
#include <bits/stdc++.h>
using namespace std;
void print_patt(int row, int curr_row,
int count)
{
// If row is less than 0, it
// returns return.
if (row <= 0)
return;
// The loop maintains the number
// of rows.
for (int i = 1; i <= curr_row; i++)
{
// To print the numbers
cout << count << " ";
// To keep increasing the count
// of numbers
count += 1;
}
// To proceed to next line.
cout << "\n";
// To increasing the current row
curr_row += 1;
// Calling the function recursively.
print_patt(row - 1,
curr_row, count);
}
// Driver Code
int main()
{
int curr_row = 1;
int count = 1;
int row = 5;
print_patt(row, curr_row, count);
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15