C++ Program To Print Floyd's Triangle Last Updated : 22 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Floyd's triangle is a triangular array of natural numbers and it is named after Robert Floyd, a renowned computer scientist popularly known for the design of the Floyd–Warshall algorithm. Here, we will see how to print Floyd's pattern triangle pyramid using the C++ program. Below are the examples: Input: row = 5Output:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Input: row = 4Output:1 2 3 4 5 6 7 8 9 10 Algorithm: At first, take the number of rows as input.The next step is to implement the nested loop to print this pattern.A variable name count maintains the value of natural numbers to be printed. There are 3 ways to print Floyd's pattern triangle pyramid: Using for loop.Using While loop.Using Recursion.Let's start discussing each of these methods in detail. 1. Using for loopBelow is the C++ program to print Floyd's pattern triangle using for loop: C++ // C++ program to print // Floyd's pattern triangle // using for loop #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; } Output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Complexity: O(n2) as the nested loop is used.Auxiliary Space: O(1) no extra space is required, so space is constant.2. Using while loopBelow is the C++ program to print Floyd's triangle using the While loop. C++ // C++ program to print // Floyd's pattern triangle // using while loop #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; } Output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Complexity: O(n2) as the nested loop is used.Auxiliary Space: O(1) no extra space is required, so space is constant.3. Using RecursionBelow is the C++ program to print Floyd's triangle using Recursion: C++ // C++ program to print // Floyd's pattern triangle // using recursion #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; } Output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Complexity: O(n2).Auxiliary Space: O(n) as a recursive approach is used. Comment More infoAdvertise with us S shoumikmanna Follow Improve Article Tags : C++ Programs C++ C Pattern Programs Practice Tags : CPP Similar Reads C++ Program To Print Pyramid Patterns In this article, we will discuss the following top 16 pattern programs in C++ using star ( * ), numbers or other characters. Table of ContentSimple Pyramid Pattern in C++Flipped Simple Pyramid Pattern in C++Inverted Pyramid Pattern in C++Flipped Inverted Pyramid Pattern in C++Triangle Pattern in C++ 15+ min read C++ Program To Print Right Half Pyramid Pattern Here we will build a C++ Program To Print Right Half Pyramid Pattern with the following 2 approaches: Using for loop Using while loop Input: rows = 5 Output: * * * * * * * * * * * * * * * 1. Using for loop First for loop is used to identify the number of rows and the second for loop is used to ident 2 min read C++ Program To Print Left Half Pyramid Pattern Here, we will build a C++ program to print the left half of pyramid pattern using 2 approaches i.e. Using for loopUsing while loop1. Using for loop Input: rows = 5 Output: * ** *** **** ***** First, for loop is used to identify the number of rows and the second for loop is used to identify the numbe 3 min read C++ Program To Print Triangle Pattern Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here: Right Triangle.Inverted Right Triangle.Equilateral Triangle.Inverted Equilateral Triangle.Inverted Mirrored Right Triangle. Let's start discussing each of these in detail. 1. Right Triangle Belo 6 min read C++ Program To Print Inverted Pyramid Here we will build a C++ Program To Print Inverted Pyramid using 3 different approaches: Half Inverted Using "*"Half Inverted Using NumbersFull Inverted Pyramid using " * "1. Program to print inverted half Triangle using " * " Input: n=4 Output: * * * * * * * * * * As we can observe from the given e 3 min read C++ Program To Print Number Pattern Here, we will see a C++ program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.3 Different Number Patterns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 1 12 123 1234 12345Pattern 1:In 6 min read C++ Program To Print Number Without Reassigning Here, we will build a C++ program to print the number pattern without Reassigning using 2 approaches i.e. Using for loopUsing while loop1. Using for loop Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The first for loop is used to iterate the number of rows and the second for loop is used 3 min read C++ Program To Print Hollow Star Pyramid Diamond Shape Pattern Here, we will build a C++ program to print the hollow star pyramid diamond shape pattern that can be achieved with two approaches i.e. Using for LoopUsing while loop Input: n = 5 Output: * * * * * * * * * * * * * * * *1. Using for loop C++ // C++ program to print hollow diamond pattern #include < 3 min read C++ Program to Print Cross or X Pattern Given a number n, we need to print an X pattern of size n. Input : n = 3Output : $ $ $ $ $Input : n = 5Output : $ $ $ $ $ $ $ $ $Input : n = 4Output : $ $ $$ $$ $ $ We need to print n rows and n columns. So we run two nested loops. The outer loop prints all rows one by one (runs for i = 1 to n). The 2 min read C++ Program to Print the Pattern 'G" In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program, 2 min read Like