-
Factorials-more
Help please. It won't output the right numbers.
Code:
#include <iostream.h>
int main()
{
float a,b,c,d,e,f,g;
a,b,c,d,e,f,g = 1;
float factorial;
cout << "Push a number that you would like to factorialize." << endl;
cin >> factorial;
cout << "You chose " << factorial << endl;
float factwhile;
factwhile = factorial;
while(factwhile > 0)
{
a = factorial - 1;
factwhile = factwhile - 1;
b = factorial - 2;
factwhile = factwhile - 1;
c = factorial - 3;
factwhile = factwhile - 1;
d = factorial - 4;
factwhile = factwhile - 1;
e = factorial - 5;
factwhile = factwhile - 1;
f = factorial - 6;
factwhile = factwhile - 1;
g = factorial - 7;
factwhile = factwhile - 1;
}
float answer;
answer = a*b*c*d*e*f*g;
cout << answer << endl;
cin.get();
return 0;
}
-
Im not sure if that is how you have to write your program for a class or whatever but I wrote this factorial function along with a combination function and permutation but the combination one is buggy yet.
Code:
int factl (int n)
{
int product = 1;
while (n>=1)
{
product *= n;
n -= 1;
}
thats the factorial one seems to work fine for me.
here is the permutation (it uses the above function):
Code:
int nPr (int n, int r)
{
long double nPr = factl(n)/factl(n-r);
}
i dont know if that helped at all, i never really thought anyone would be seeing it so its not commented but any questions just ask, ill check back frequently.
edit: you can only do up to 12! because thats all it can handle but since i cant get my if statements working in these functions right now I cant fix it to not accept a value > 12.
-
A few errors:
Code:
int factl (int n)
{
int product = 1;
while (n>=1)
{
product *= n;
n -= 1;
}
return product;
}
Code:
long nPr (int n, int r)
{
return factl(n)/factl(n-r);
}
And for choosing, its
Code:
long nCr (int n, int r)
{
return factl(n)/(factl(n-r) * factl(r));
return nCr;
}