fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6. int rows;
  7. cin >> rows;
  8.  
  9. cout << "Butterfly Pattern of rows "<<rows<< endl;
  10.  
  11. // Main logic to print the Butterfly pattern.
  12. // Printing upper part.
  13. for( int i = 0; i <= rows; i++ ){
  14. // Print left side stars.
  15. for( int j = 0; j <= i; j++ ){
  16. cout << "* ";
  17. }
  18.  
  19. // Print spaces.
  20. int spaces = 2 * (rows - i);
  21. for( int j = 0; j < spaces; j++){
  22. cout << " ";
  23. }
  24.  
  25. // Print right side stars.
  26. for( int j = 0; j <= i; j++ ){
  27. cout << "* ";
  28. }
  29.  
  30. cout << endl;
  31. }
  32.  
  33. // Printing bottom part.
  34. for( int i = rows - 1; i >= 0; i-- ){
  35.  
  36. // Print left side spaces.
  37. for( int j = 0; j <= i; j++ ){
  38. cout << "* ";
  39. }
  40.  
  41. // Print spaces.
  42. int spaces = 2 * (rows - i);
  43. for( int j = 0; j < spaces; j++){
  44. cout << " ";
  45. }
  46.  
  47. // Print right side stars.
  48. for( int j = 0; j <= i; j++ ){
  49. cout << "* ";
  50. }
  51.  
  52. cout << endl;
  53. }
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0.01s 5276KB
stdin
6
stdout
Butterfly Pattern of rows 6
*                         * 
* *                     * * 
* * *                 * * * 
* * * *             * * * * 
* * * * *         * * * * * 
* * * * * *     * * * * * * 
* * * * * * * * * * * * * * 
* * * * * *     * * * * * * 
* * * * *         * * * * * 
* * * *             * * * * 
* * *                 * * * 
* *                     * * 
*                         *