-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecursive.cpp
More file actions
30 lines (25 loc) · 902 Bytes
/
recursive.cpp
File metadata and controls
30 lines (25 loc) · 902 Bytes
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
#include <iostream> //preprocessor directive
using namespace std;
int computeFactorials (int,int); //prototype declaration
// int factorial (int);
inline int factorial (int n) { //inline function to keep from checking with function
return (n==1) ? 1 : (factorial(n-1) * n); //recursive function using ternary operator
}
int main() {
computeFactorials(1,8); //call function
return 0;
}
int computeFactorials (int num, int max) { //first prototype function
cout << "Factorial of " << num << ": ";
cout << factorial(num) << endl; //statements
num++; //incrementer
if (num > max) return 0; //exit...
else computeFactorials(num, max); //recursive call
}
// int factorial (int n) // second prototype function
// {
// int result;
// if (n==1) result = 1;
// else result = (factorial(n-1) * n); //exit or decrement and call again
// return result;
// }