In this article, you will learn how to print the Butterfly Pattern in C++ language of the numbers using for loop statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
-----Enter the height of the pattern----- 6 -----This the butterfly pattern----- 1 1 1 2 2 1 1 2 3 3 2 1 1 2 3 4 4 3 2 1 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 6 6 5 4 3 2 1 1 2 3 4 5 5 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 3 2 1 1 2 2 1 1 1 |
You should have knowledge of the following topics in c++ programming to understand this program:
- C++
main()
function - C++
cin
object - C++
cout
object - C++
for
loop statement - C++
putchar()
function
In this program, we used normal functions and statements to print the butterfly pattern in C++ language.
C++ 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 |
// Butterfly Pattern in C++ language of the numbers using for loop #include <iostream> using namespace std; int main() { int r, h, d, s; // r - denotes for pattern row // h - denotes for pattern height // d - denotes for digits // s - denotes for space cout << "-----Enter the height of the pattern-----\n"; cin >> h; cout << "\n-----This the butterfly pattern-----\n\n\n"; for(r = 1; r <= h - 1; r++) { cout << "\t"; for(d = 1; d <= r; d++) cout << d; for(s = 1; s <= 2 * (h - r); s++) cout << " "; putchar('\b'); for(d = r; d >= 1; d--) cout << d; putchar('\n'); } for(r = h; r >= 1; r--) { cout << "\t"; for(d = 1; d <= r; d++) cout << d; for(s = 1; s <= 2 * (h - r); s++) cout << " "; putchar('\b'); for(d = r; d >= 1; d--) cout << d; putchar('\n'); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
-----Enter the height of the pattern----- 6 -----This the butterfly pattern----- 1 1 1 2 2 1 1 2 3 3 2 1 1 2 3 4 4 3 2 1 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 6 6 5 4 3 2 1 1 2 3 4 5 5 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 3 2 1 1 2 2 1 1 1 |
In this program, we have taken input 6
size of the pattern then made the calculation using the for
loop statement & putchar()
function.