Hey everyone, first time poster in here. I'm having some difficulty with this program. I need to write a program to compute C(n,r) = n! / r!(n-r)!. Normally, I would use three 'for' statements, but my professor wants us to use one function to calculate all three factorials. Here's what I have so far:
#include (iostream) <-- in parenthesis to show up on screen
using namespace std;
int factorial(int n, int r);
int main ()
{
int n, r, answer = 1;
cout << "Enter two variables, n and r ";
cin >> n >> r;
answer = factorial(n , 0) / (factorial (r) * factorial (n - r));
return 0;
}
int factorial (int n, int r)
{
int i, product = n;
for (i = n; i > 1; --i)
{
product = product * i;
}
return product;
}
I understand how to write programs, but assigning numerous values to the factorial confuses me. Any help would be greatly appreciated.
#include (iostream) <-- in parenthesis to show up on screen
using namespace std;
int factorial(int n, int r);
int main ()
{
int n, r, answer = 1;
cout << "Enter two variables, n and r ";
cin >> n >> r;
answer = factorial(n , 0) / (factorial (r) * factorial (n - r));
return 0;
}
int factorial (int n, int r)
{
int i, product = n;
for (i = n; i > 1; --i)
{
product = product * i;
}
return product;
}
I understand how to write programs, but assigning numerous values to the factorial confuses me. Any help would be greatly appreciated.
