Number Pattern: Four Triangles with Floyd's in C++
Print four number triangles in C++: counting up, repeated row numbers, counting down, and Floyd's triangle with num++. Same loops, four different print values.
1#include <iostream>2using namespace std;34int main() {5 int rows;67 cout << "Enter number of rows: ";8 cin >> rows;910 // Pattern 1: Increasing numbers11 cout << "\nPattern 1:" << endl;12 for (int i = 1; i <= rows; i++) {13 for (int j = 1; j <= i; j++) {14 cout << j << " ";15 }16 cout << endl;17 }1819 // Pattern 2: Same number in row20 cout << "\nPattern 2:" << endl;21 for (int i = 1; i <= rows; i++) {22 for (int j = 1; j <= i; j++) {23 cout << i << " ";24 }25 cout << endl;26 }2728 // Pattern 3: Decreasing numbers29 cout << "\nPattern 3:" << endl;30 for (int i = 1; i <= rows; i++) {31 for (int j = i; j >= 1; j--) {32 cout << j << " ";33 }34 cout << endl;35 }3637 // Pattern 4: Floyd's Triangle38 cout << "\nPattern 4 (Floyd's Triangle):" << endl;39 int num = 1;40 for (int i = 1; i <= rows; i++) {41 for (int j = 1; j <= i; j++) {42 cout << num++ << " ";43 }44 cout << endl;45 }4647 return 0;48}
Sample Output
5 5 5 5 5
Pattern 2, row 5: the row number repeated.
11 12 13 14 15
Floyd's triangle, row 5: the count continues.
In this one
- How printing j, i, or a countdown changes a triangle
- How a counter declared outside both loops creates Floyd's triangle
- What num++ prints and then does
- How to spot the one line that differs between patterns
How it works
- 1
Read rows (5).
- 2
Pattern 1 prints j: 1 / 1 2 / ...
- 3
Pattern 2 prints i: 1 / 2 2 / 3 3 3 ...
- 4
Pattern 3 counts j from i down to 1.
- 5
Pattern 4 prints num++ with num declared once, so values run 1 to 15.
Edge cases
Resetting num
Move int num = 1; inside the outer loop.
Each row of Floyd's triangle restarts at 1, so it turns into Pattern 1 instead.
++num instead of num++
Write cout << ++num.
The increment happens before printing, so the triangle starts at 2 and ends at 16.
What's going on
This page is a set of four number triangles printed one after another, ending with Floyd's triangle, where the numbers keep counting across rows instead of starting over.
Four number triangles, same nested-loop skeleton, different thing in cout. Sample rows = 5.
Pattern 1 — cout << j: 1 / 1 2 / 1 2 3 / … That's the beginner Number Pattern page.
Pattern 2 — cout << i: 1 / 2 2 / 3 3 3 / 4 4 4 4 / 5 5 5 5 5. The row index, repeated.
Pattern 3 — j from i down to 1: 1 / 2 1 / 3 2 1 / … Last cell of the row is always 1.
Pattern 4 — Floyd. int num = 1; outside both loops, then cout << num++. Numbers keep going: 1 / 2 3 / 4 5 6 / 7 8 9 10 / 11 12 13 14 15.
Put num = 1 inside the outer loop and Floyd dies — every row starts at 1 again.
The loops are identical: i is the row, j <= i is how many cells. Only the printed value changes. That's the whole trick.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does cout << num++ print when num is 7?
Pick an answer to check it.
Question 2
In Pattern 3 for row 4, what prints?
Pick an answer to check it.
Questions people ask
What is Floyd's triangle?
A right triangle filled with consecutive numbers: 1, then 2 3, then 4 5 6, and so on.
What is the last number in Floyd's triangle?
rows(rows+1)/2. For 5 rows that is 15.
Why does num live outside the loops?
So its value survives from one row to the next.