fork download
  1. // C++ Program to print the inverted triangle pattern
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // function to print inverted triangle
  6. void printInvTriangle(int n)
  7. {
  8.  
  9. // outer loop to go thruough every row
  10. for (int i = 0; i < n; i++) {
  11.  
  12. // leading space counter
  13. int space = i;
  14.  
  15. // inner loop to print space and star * in each row
  16. for (int j = 0; j < 2 * n - i - 1; j++) {
  17.  
  18. // condition to check weather the star * or
  19. // whitespace is to be printed
  20. if (space) {
  21. cout << " ";
  22. space--;
  23. }
  24. else {
  25. cout << "* ";
  26. }
  27. }
  28. cout << endl;
  29. }
  30. }
  31.  
  32. // driver code
  33. int main()
  34. {
  35. printInvTriangle(5);
  36.  
  37. return 0;
  38. }
  39.  
  40.  
Success #stdin #stdout 0s 5304KB
stdin
6
stdout
* * * * * * * * * 
  * * * * * * * 
    * * * * * 
      * * * 
        *