In this C++ program we will print a Floyd Triangle of N rows. A Floyd’s triangle is a right angled triangle of natural numbers arranged in increasing order from left to right such that Nth row contains N numbers.
A floyd's triangle of 6 rows :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
In this program, we first take number of rows of Floyd's triangle as input from user and store it in a variable rows. Then using two for loops we will print N consecutive natural numbers in Nth row. Here outer for loop prints one row in every iteration where as inner for loop prints numbers of one row. Between any two consecutive numbers in a line we will print a space character.
Here we are using for loop but same program can be rewritten using while loop or do while loop.
C++ Program to Print Floyd Triangle
#include <iostream> using namespace std; int main() { int i, j, rows, counter; cout << "Enter the number of rows of Floyd's triangle\n"; cin >> rows; // Print Floyd's triangle for (counter = 1, i = 1; i <= rows; i++) { // Print ith row for (j = 1; j <= i; j++) { cout << counter++ << " "; } cout << endl; } return 0; }Output
Enter the number of rows of Floyd's triangle 4 1 2 3 4 5 6 7 8 9 10
Recommended Posts