-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfunction_factorial.cpp
64 lines (57 loc) · 1.04 KB
/
function_factorial.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
54
55
56
57
58
59
60
61
62
63
/*
Program: function_factorial.cpp
Calculates factorial of a number (e.g., 10! )
Compile: g++ -o function_factorial.x function_factorial.cpp -O2
*/
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <iomanip>
using namespace std;
#define XTAB '\t'
// Factorial function prototype.............................
long factorial( long a );
// Main program.............................................
int main(){
long i;
long n;
string s;
long r;
stringstream ss;
cout << "Please type an integer: ";
getline(cin,s);
ss.clear();
ss << s;
ss >> n;
for ( i = 1 ; i <= n; i++ ){
r = factorial(i);
cout << i << XTAB << r << endl;
}
return 0;
}
// Factorial function.......................................
long factorial( long a ){
long r;
if ( a > 1 ){
r = a * factorial( a - 1 );
return( r );
}
else{
return(1);
}
}
/*
Example output:
Please type an integer: 10
10 1
10 2
10 6
10 24
10 120
10 720
10 5040
10 40320
10 362880
10 3628800
*/