garryhtreez

5.21

Nov 19th, 2020 (edited)
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. /*  5.21 Modified Triangle-Printing Program
  2.     Deitel & Deitel C++ How to Program, 10th ed. (Indian subcontinent adaptation)
  3.     Visual Studio Community 2019
  4. */
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. int main() {
  10.  
  11.    int y, row;
  12.    int const MAX = 10;
  13.  
  14.    for ( row = 1; row <= MAX; row++ ) {
  15.  
  16.       // print asterisks for 1st triangle
  17.       for ( y = 1; y <= row; y++ )
  18.          cout << "*";
  19.  
  20.       // print spaces for 1st triangle
  21.       for ( y = row + 1; y <= MAX; y++ )
  22.          cout << " ";
  23.  
  24.       cout << " ";
  25.  
  26.       // print asterisks for 2nd triangle
  27.       for ( y = 0; y <= ( MAX - row ); y++ )
  28.          cout << "*";
  29.  
  30.       // print spaces for 2nd triangle
  31.       for ( y = 1; y < row; y++ )
  32.          cout << " ";
  33.  
  34.       cout << " ";
  35.  
  36.       // print spaces for 3rd triangle
  37.       for ( y = 1; y < row; y++ )
  38.          cout << " ";
  39.  
  40.       // print asterisks for 3rd triangle
  41.       for ( y = 0; y <= ( MAX - row ); y++ )
  42.          cout << "*";
  43.  
  44.       cout << " ";
  45.  
  46.       // print spaces for 4th triangle
  47.       for (y = row + 1; y <= MAX; y++)
  48.          cout << " ";
  49.  
  50.       // print asterisks for 4th triangle
  51.       for ( y = 1; y <= row; y++ )
  52.          cout << "*";
  53.  
  54.       cout << "\n"; // carriage return then back to loop
  55.    }
  56.  
  57.    return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment