fork download
  1. // C++ code to demonstrate triangle pattern
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // Function to demonstrate printing pattern
  6. void simpleTriangle(int n)
  7. {
  8. // Number of spaces
  9. int i, j, k = n;
  10.  
  11. // Outer loop to handle number of rows
  12. // n in this case
  13. for (i = 1; i <= n; i++) {
  14.  
  15. // Inner loop for columns
  16. for (j = 1; j <= n; j++) {
  17.  
  18. // Condition to print star pattern
  19. if (j >= k)
  20. cout << "* ";
  21. else
  22. cout << " ";
  23. }
  24. k--;
  25. cout << "\n";
  26. }
  27. }
  28.  
  29. // Driver Code
  30. int main()
  31. {
  32. int n = 5;
  33. // Function Call
  34. simpleTriangle(n);
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
    * 
   * * 
  * * * 
 * * * * 
* * * * *