-
Notifications
You must be signed in to change notification settings - Fork 1
/
pascal's-triangle.cpp
53 lines (36 loc) · 1020 Bytes
/
pascal's-triangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Pascals triangle
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int numberOfRows;
int i , s , a , j , num = 1 , sum = 0;
int product=1;
cout << "This program displays Pascals' Triangle for n number of rows \n";
cout << "Enter the Number of Rows you want: ";
cin >> numberOfRows;
for( i = 1 ; i <= numberOfRows ; i++ ){
//for space
for( s = numberOfRows - 1 ; s >= i ; s--){
cout << " ";
}
cout << 1;
//for the rest of the numbers
for ( j = 1 ; j < i ; j++ ){
cout << " " ;
num = num * ( i - j ) / (j);
cout << num << " ";
sum += num;
product *=num;
}
cout << endl;
}
cout<<endl;
//sum. each row sums to 2^(row num -1)
cout<<"The sum of "<<numberOfRows<<" number of Rows is "<<sum + numberOfRows<<endl;
// adding number of rows to sum to account for the ones at the front of each row
cout<<"The product of "<<numberOfRows<<" number of Rows is "<<product<<endl;
/*
*/
return 0;
}