In this tutorial, we will learn how to print the Cpp program to pyramid star in a given range using for loop and while loop
You may also like: Cpp program to pyramid numbers in a given range
pyramid number pattern programs in C++language
Pattern:
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 41 42 43 44 | #include <iostream> using namespace std; int main() { int end, count = 0, count1 = 0, k = 0, begin; cout << "Enter number of begin: "; cin >> begin; cout << "Enter number of end: "; cin >> end; for(int i = begin; i <= end; ++i) { for(int space = 1; space <= end-i; ++space) { cout << " "; ++count; } while(k != 2*i-1) { if (count <= end-1) { cout << "*" << " "; ++count; } else { ++count1; cout << "*" << " "; } ++k; } count1 = count = k = 0; cout << endl; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 | Enter number of begin: 5 Enter number of end: 10 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
[…] You may also like: Cpp program to star pyramid in a given range […]