I have a program that prints out pascal's triangle. One problem: it isn't a triangle. The output doesn't work. This is what it should print:
Code:
How many rows: 4
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
and this is what it does print:
Code:
Enter a number of rows: 4
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Can someone help me get the spacing right? My code is:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int combinations (int n, int k) {
if (k == 0 || k == n) {
return 1;
}
else {
return combinations(n-1,k-1) + combinations(n-1,k);
}
}
int main ( ) {
int rows;
cout << "Enter a number of rows: ";
cin >> rows;
for(int r = 0; r < rows+1; r++) {
cout << " " << "1";
for(int c = 1; c < r+1; c++) {
cout << " " << combinations(r, c) << ' ';
}
cout << endl;
}
}