This is another example of a
recursive function. It calculates the combinatorial:
using namespace System;
long Factorial(long x)
{
if( x <= 1 )
return 1;
else
return x * Factorial(x - 1);
}
long Combinatorial(long a, long b)
{
if( a <= 1 )
return 1;
return Factorial(a) / (Factorial(b) * Factorial(a - b));
}
int main()
{
const long Number = 5;
Console::WriteLine(L"The factorial of {0} is {1}",
Number, Factorial(Number));
Console::WriteLine(L"The combinatorial of (8, 3) is {0}",
Combinatorial(8, 3));
return 0;
}
This would produce:
The factorial of 5 is 120
The combinatorial of (8, 3) is 56
Press any key to continue . . .
|
|