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