fork download
  1. //
  2. // main.cpp
  3. // Pascal Triangle
  4. //
  5. // Created by Himanshu on 20/09/21.
  6. //
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11.  
  12. void printPascalTriangle (int n) {
  13.  
  14. //Base case
  15. cout<<"1"<<endl;
  16.  
  17. for (int i=1; i<n; i++) {
  18. int C = 1;
  19. for (int j=0; j<=i; j++) {
  20.  
  21. cout<<C<<" ";
  22. //We are using (i-j) instead of (i-(j-1)) because
  23. //calculation is for next or (j+1)th element
  24. C = C * (i-j)/(j+1);
  25. }
  26. cout<<endl;
  27. }
  28.  
  29. }
  30.  
  31. int main() {
  32. int n = 7;
  33. printPascalTriangle (n);
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5352KB
stdin
Standard input is empty
stdout
1
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1