fork download
  1. // HW week1 DSA course
  2. //1.Print triangles using *, numbers, and characters
  3. //2.Rectangle Pattern
  4. //3. Hollow Rectangle Pattern
  5. //4. Inverted Half Pyramid
  6. //5. Floyd’s Triangle
  7. //6. Butterfly Pattern
  8. //7. Print Pascal's triangle
  9. // 8.Print Full Pyramid Pattern of Stars
  10.  
  11.  
  12.  
  13. #include<iostream>
  14. using namespace std;
  15.  
  16. int main(){
  17.  
  18. // 1.Print triangles using *, numbers, and characters
  19.  
  20. /* int rows;
  21.   // Getting the number of rows.
  22.   cout << "Enter the Number of rows - ";
  23.   cin >> rows;
  24.  
  25.   cout << "Triangle of " << rows << " using * -\n";
  26.  
  27.   for( int i = 0; i < rows; i++ ) {
  28.   for( int j = 0; j <= i; j++ ){
  29.   cout << "* ";
  30.   }
  31.   cout<<endl;
  32.   }*/
  33.  
  34.  
  35. //2.Rectangle Pattern
  36.  
  37. /*int rows, cols;
  38.   // Getting dimensions of the rectangle.
  39.   cout << "Enter the number of rows in rectangle - ";
  40.   cin >> rows;
  41.  
  42.   cout << "Enter the number of columns in rectangle - ";
  43.   cin >> cols;
  44.  
  45.  
  46.   cout << "Rectangle of dimensions " << rows << "*" << cols << endl;
  47.  
  48.   for( int i = 0; i < rows; i++ ) {
  49.   for( int j = 0; j < cols; j++ ){
  50.   cout << "* ";
  51.   }
  52.   cout<<endl;
  53.   }*/
  54.  
  55. //3. Hollow Rectangle Pattern
  56.  
  57. /* int rows, cols;
  58.   // Getting dimensions of rectangle.
  59.   cout << "Enter the number of rows in rectangle - ";
  60.   cin >> rows;
  61.  
  62.   cout << "Enter the number of columns in rectangle - ";
  63.   cin >> cols;
  64.  
  65.   cout << "Rectangle of dimensions " << rows << "*" << cols << endl;
  66.  
  67.   // Main logic to print hollow rectangle.
  68.   for (int i = 0; i < rows; i++) {
  69.   for (int j = 0; j < cols; j++) {
  70.  
  71.   // If the index is at the border, then print *.
  72.   if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1)
  73.   cout << "* ";
  74.   else
  75.   cout << " ";
  76.   }
  77.   cout << endl;
  78.   }*/
  79.  
  80. //4. Inverted Half Pyramid
  81.  
  82. /* int rows;
  83.   // Getting the number of rows.
  84.   cout << "Enter the Number of rows - ";
  85.   cin >> rows;
  86.  
  87.   cout << "Floyd's Triangle of " << rows << " rows." << endl;
  88.  
  89.   // Main logic to print Floyd's Triangle.
  90.   int counter = 1;
  91.   for( int i = 0; i < rows; i++ ) {
  92.   for( int j = 0; j <= i; j++ ){
  93.   cout << (counter++) << " ";
  94.   }
  95.   cout << endl;
  96.   }*/
  97.  
  98.  
  99. //5. Floyd’s Triangle
  100.  
  101. /* int rows;
  102.   // Getting the number of rows.
  103.   cout << "Enter the Number of rows - ";
  104.   cin >> rows;
  105.  
  106.   cout << "Floyd's Triangle of " << rows << " rows." << endl;
  107.  
  108.   // Main logic to print Floyd's Triangle.
  109.   int counter = 1;
  110.   for( int i = 0; i < rows; i++ ) {
  111.   for( int j = 0; j <= i; j++ ){
  112.   cout << (counter++) << " ";
  113.   }
  114.   cout << endl;
  115.   }*/
  116.  
  117. //6. Butterfly Pattern
  118.  
  119. /*int rows;
  120.   // Getting the number of rows.
  121.   cout << "Enter the Number of rows - ";
  122.   cin >> rows;
  123.  
  124.   cout << "Butterfly Pattern of " << rows << " rows." << endl;
  125.  
  126.   // Main logic to print the Butterfly pattern.
  127.   // Printing upper part.
  128.   for( int i = 0; i <= rows; i++ ){
  129.   // Print left side stars.
  130.   for( int j = 0; j <= i; j++ ){
  131.   cout << "* ";
  132.   }
  133.  
  134.   // Print spaces.
  135.   int spaces = 2 * (rows - i);
  136.   for( int j = 0; j < spaces; j++){
  137.   cout << " ";
  138.   }
  139.  
  140.   // Print right side stars.
  141.   for( int j = 0; j <= i; j++ ){
  142.   cout << "* ";
  143.   }
  144.  
  145.   cout << endl;
  146.   }
  147.  
  148.   // Printing bottom part.
  149.   for( int i = rows - 1; i >= 0; i-- ){
  150.  
  151.   // Print left side spaces.
  152.   for( int j = 0; j <= i; j++ ){
  153.   cout << "* ";
  154.   }
  155.  
  156.   // Print spaces.
  157.   int spaces = 2 * (rows - i);
  158.   for( int j = 0; j < spaces; j++){
  159.   cout << " ";
  160.   }
  161.  
  162.   // Print right side stars.
  163.   for( int j = 0; j <= i; j++ ){
  164.   cout << "* ";
  165.   }
  166.  
  167.   cout << endl;
  168.   }*/
  169.  
  170. //7. Print Pascal's triangle
  171.  
  172. /* int rows;
  173.   // Getting the number of rows.
  174.   cout << "Enter the Number of rows - ";
  175.   cin >> rows;
  176.  
  177.   cout << "Pascal's Triangle of " << rows << " rows." << endl;
  178.  
  179.   // Main logic to print Pascal's triangle.
  180.   for( int i = 0; i < rows; i++){
  181.   int spaces = rows - i;
  182.   // Print spaces.
  183.   for( int j = 0; j < spaces; j++){
  184.   cout<<" ";
  185.   }
  186.  
  187.   int coefficient;
  188.   // Print values.
  189.   for( int j = 0; j <= i; j++){
  190.   // Update coefficient's value
  191.   if( j == 0 )
  192.   coefficient = 1;
  193.   else
  194.   coefficient = coefficient * (i - j + 1) / j;
  195.  
  196.   cout << coefficient << " ";
  197.   }
  198.  
  199.   cout << endl;
  200.   }*/
  201.  
  202.  
  203.  
  204. // 8.Print Full Pyramid Pattern of Stars
  205.  
  206. int rows;
  207. // Getting the number of rows.
  208. cout << "Enter the Number of rows - ";
  209. cin >> rows;
  210.  
  211. cout << "Full Pyramid of " << rows << " using * -\n";
  212.  
  213. // Main logic to print full pyramid.
  214. for( int i = 0; i < rows; i++ ) {
  215. // Print spaces.
  216. int spaces = rows - i;
  217.  
  218. for( int j = 0; j < spaces; j++){
  219. cout <<" ";
  220. }
  221.  
  222. // Print stars.
  223. for( int j = 0; j < 2 * i - 1; j++){
  224. cout <<"* ";
  225. }
  226.  
  227. cout << endl;
  228.  
  229. }
  230.  
  231.  
  232. return 0;
  233. }
  234.  
Success #stdin #stdout 0.01s 5300KB
stdin
8

stdout
Enter the Number of rows - Full Pyramid of 8 using * -
                
              * 
            * * * 
          * * * * * 
        * * * * * * * 
      * * * * * * * * * 
    * * * * * * * * * * * 
  * * * * * * * * * * * * *