fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void halfPyramid();
  6. void fullPyramid();
  7. void rectangle();
  8. void hollowRectangle();
  9. void butterfly();
  10.  
  11. int main() {
  12. int choice;
  13.  
  14. // Give options to the user.
  15. cout << "!!! Press 0 to Exit !!!"<<endl;
  16. cout << "1. Print Half Pyramid of Stars"<<endl;
  17. cout << "2. Print Full Pyramid of Stars"<<endl;
  18. cout << "3. Print Rectangle of Stars"<<endl;
  19. cout << "4. Print Hollow Rectangle of Stars"<<endl;
  20. cout << "5. Print Butterefly of stars"<<endl;
  21.  
  22. do {
  23. // Take input of choice
  24. cin>>choice;
  25. cout << "\nEnter the Choice: "<<choice<<endl;
  26.  
  27. switch (choice) {
  28. // If he entered 0, exit.
  29. case 0:
  30. return 0;
  31. // Print half pyramid on entering 1.
  32. case 1:
  33. halfPyramid();
  34. break;
  35. // Print full pyramid on entering 2.
  36. case 2:
  37. fullPyramid();
  38. break;
  39. // Print rectangle on entering 3.
  40. case 3:
  41. rectangle();
  42. break;
  43. // Print hollow rectangle on entering 4.
  44. case 4:
  45. hollowRectangle();
  46. break;
  47. // Print butterfly on entering 5.
  48. case 5:
  49. butterfly();
  50. break;
  51. // If the choice doesn't match, print the following message
  52. default:
  53. cout << "\n !!! Incorrect Choice !!!";
  54. }
  55. } while (choice != 0);
  56. return 0;
  57. }
  58.  
  59. void halfPyramid() {
  60. int rows;
  61. cin >> rows;
  62.  
  63. cout <<"Triangle of "<<rows<<" using *:"<<endl;
  64.  
  65. // Main logic to print triangle.
  66. for (int i = 0; i < rows; i++) {
  67. for (int j = 0; j <= i; j++) {
  68. cout << "* ";
  69. }
  70. cout << endl;
  71. }
  72. }
  73.  
  74. void fullPyramid() {
  75. int rows;
  76. cin >> rows;
  77.  
  78. cout << "Full Pyramid of "<<rows<<" using *"<<endl;
  79.  
  80. // Main logic to print full pyramid.
  81. for (int i = 0; i < rows; i++) {
  82. int spaces = rows - i;
  83. for (int j = 0; j < spaces; j++) {
  84. cout << " ";
  85. }
  86.  
  87. for (int j = 0; j < 2 * i + 1; j++) {
  88. cout << "* ";
  89. }
  90. cout << endl;
  91. }
  92. }
  93.  
  94. void rectangle() {
  95. int rows, cols;
  96. cin >> rows;
  97. cin >> cols;
  98.  
  99. cout << "Rectangle of dimensions "<<rows<<"x"<<cols<<endl;
  100.  
  101. // Main logic to print Rectangle.
  102. for (int i = 0; i < rows; i++) {
  103. for (int j = 0; j < cols; j++) {
  104. cout << "* ";
  105. }
  106. cout << endl;
  107. }
  108. }
  109.  
  110. void hollowRectangle() {
  111. int rows, cols;
  112. cin >> rows;
  113. cin >> cols;
  114.  
  115. cout << "Rectangle of dimensions "<<rows<<"x"<<cols<<endl;
  116.  
  117. // Main logic to print hollow rectangle.
  118. for (int i = 0; i < rows; i++) {
  119. for (int j = 0; j < cols; j++) {
  120.  
  121. // If the index is at the border, then print *.
  122. if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1)
  123. cout << "* ";
  124. else
  125. cout << " ";
  126. }
  127. cout << endl;
  128. }
  129. }
  130.  
  131. void butterfly(){
  132. int rows;
  133. cin >> rows;
  134. cout << "Butterfly Pattern of rows "<<rows<< endl;
  135.  
  136. // Main logic to print the Butterfly pattern.
  137. // Printing upper part.
  138. for( int i = 0; i <= rows; i++ ){
  139. // Print left side stars.
  140. for( int j = 0; j <= i; j++ ){
  141. cout << "* ";
  142. }
  143. // Print spaces.
  144. int spaces = 2 * (rows - i);
  145. for( int j = 0; j < spaces; j++){
  146. cout << " ";
  147. }
  148. // Print right side stars.
  149. for( int j = 0; j <= i; j++ ){
  150. cout << "* ";
  151. }
  152. cout << endl;
  153. }
  154.  
  155. // Printing bottom part.
  156. for( int i = rows - 1; i >= 0; i-- ){
  157. // Print left side spaces.
  158. for( int j = 0; j <= i; j++ ){
  159. cout << "* ";
  160. }
  161.  
  162. // Print spaces.
  163. int spaces = 2 * (rows - i);
  164. for( int j = 0; j < spaces; j++){
  165. cout << " ";
  166. }
  167.  
  168. // Print right side stars.
  169. for( int j = 0; j <= i; j++ ){
  170. cout << "* ";
  171. }
  172. cout << endl;
  173. }
  174. }
  175.  
Success #stdin #stdout 0.01s 5280KB
stdin
1
4
2
4
3
4
6
4
4
6
5
6
0
stdout
!!! Press 0 to Exit !!!
1. Print Half Pyramid of Stars
2. Print Full Pyramid of Stars
3. Print Rectangle of Stars
4. Print Hollow Rectangle of Stars
5. Print Butterefly of stars

Enter the Choice: 1
Triangle of 4 using *:
*  
*  *  
*  *  *  
*  *  *  *  

Enter the Choice: 2
Full Pyramid of 4 using *
        * 
      * * * 
    * * * * * 
  * * * * * * * 

Enter the Choice: 3
Rectangle of dimensions 4x6
* * * * * * 
* * * * * * 
* * * * * * 
* * * * * * 

Enter the Choice: 4
Rectangle of dimensions 4x6
* * * * * * 
*         * 
*         * 
* * * * * * 

Enter the Choice: 5
Butterfly Pattern of rows 6
*                         * 
* *                     * * 
* * *                 * * * 
* * * *             * * * * 
* * * * *         * * * * * 
* * * * * *     * * * * * * 
* * * * * * * * * * * * * * 
* * * * * *     * * * * * * 
* * * * *         * * * * * 
* * * *             * * * * 
* * *                 * * * 
* *                     * * 
*                         * 

Enter the Choice: 0