fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int rows;
  6. cout << "Enter the number of rows: ";
  7. cin >> rows;
  8.  
  9. // Upper half of the butterfly pattern
  10. for (int i = 0; i < rows; i++) {
  11. // Print left side stars
  12. for (int j = 0; j <= i; j++) {
  13. cout << "1 ";
  14. }
  15.  
  16. // Print spaces in the middle
  17. int spaces = 2 * (rows - i - 1);
  18. for (int j = 0; j < spaces; j++) {
  19. cout << " ";
  20. }
  21.  
  22. // Print right side stars
  23. for (int j = 0; j <= i; j++) {
  24. cout << "3 ";
  25. }
  26.  
  27. cout << endl;
  28. }
  29.  
  30. // Lower half of the butterfly pattern
  31. for (int i = rows - 1; i >= 0; i--) {
  32. // Print left side stars
  33. for (int j = 0; j <= i; j++) {
  34. cout << "2 ";
  35. }
  36.  
  37. // Print spaces in the middle
  38. int spaces = 2 * (rows - i - 1);
  39. for (int j = 0; j < spaces; j++) {
  40. cout << " ";
  41. }
  42.  
  43. // Print right side stars
  44. for (int j = 0; j <= i; j++) {
  45. cout << "4 ";
  46. }
  47.  
  48. cout << endl;
  49. }
  50.  
  51. return 0;
  52. }
  53.  
  54.  
Success #stdin #stdout 0.01s 5300KB
stdin
8
stdout
Enter the number of rows: 1                             3 
1 1                         3 3 
1 1 1                     3 3 3 
1 1 1 1                 3 3 3 3 
1 1 1 1 1             3 3 3 3 3 
1 1 1 1 1 1         3 3 3 3 3 3 
1 1 1 1 1 1 1     3 3 3 3 3 3 3 
1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 
2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 
2 2 2 2 2 2 2     4 4 4 4 4 4 4 
2 2 2 2 2 2         4 4 4 4 4 4 
2 2 2 2 2             4 4 4 4 4 
2 2 2 2                 4 4 4 4 
2 2 2                     4 4 4 
2 2                         4 4 
2                             4