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