In this tutorial, we will discuss the Cpp program to pyramid number pattern
In this topic, we will learn how to create number pattern in C++ language using for loop and while loop
pyramid number pattern programs in C++language
CPP Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> using namespace std; int main() { int rows, count = 0, count1 = 0, k = 0; cout << "Enter number of rows: "; cin >> rows; for(int i = 1; i <= rows; ++i) { for(int space = 1; space <= rows-i; ++space) { cout << " "; ++count; } while(k != 2*i-1) { if (count <= rows-1) { cout << i+k << " "; ++count; } else { ++count1; cout << i+k-2*count1 << " "; } ++k; } count1 = count = k = 0; cout << endl; } return 0; } |
When the above code is compiled and executed, it produces the following results
1 2 3 4 5 6 7 8 9 | Enter number of rows: 6 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 10 11 10 9 8 7 6 |