fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int rows;
  6. cin >> rows;
  7.  
  8. cout << "Pascal's Triangle of rows "<<rows<< endl;
  9.  
  10. // Main logic to print Pascal's triangle
  11. for( int i = 0; i < rows; i++){
  12. int spaces = rows - i;
  13. // Print spaces.
  14. for( int j = 0; j < spaces; j++){
  15. cout<<" ";
  16. }
  17.  
  18. int coefficient;
  19. // Print values.
  20. for( int j = 0; j <= i; j++){
  21. // Update coefficient's value
  22. if( j == 0 )
  23. coefficient = 1;
  24. else
  25. coefficient = coefficient * (i - j + 1) / j;
  26. cout << coefficient << " ";
  27. }
  28. cout << endl;
  29. }
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5276KB
stdin
6
stdout
Pascal's Triangle of rows 6
            1   
          1   1   
        1   2   1   
      1   3   3   1   
    1   4   6   4   1   
  1   5   10   10   5   1