Hey, i am trying to write a program that will evaluate n!/(k!(n-k)!)

right now i am just trying to get the basic formula without the factorials to work ( i can just do a function call later and get the actual values) but no luck. all the variables have values but im still outputting 0....

here is my code

#include <iostream>
using namespace std;

int factorial (int);   //function prototype or declaration

int main()
{
	int n1, n2, result, counter, n(0), k(0), partialanswer, partialdenom, numerator, denominator, answer;

	cout<<"Please enter two positive integer values less than 15. "<<endl;
	cin>>n1;
	cin>>n2;

	/* while (n1 < 1 || n1 >15)
	{
		cout<<"You entered an incorrect integer value!"<<endl;
		cout<<"Please enter a positive integer value less than 15. "<<endl;
		cin>>n1;
		cin>>n2;
	}

	//result = factorial(Q);
	///result = partialanswer;
	//cout<< "The factorial of  "<<n1 <<" write out the rest "<<result<<"."<<endl;
	*/

	
	
	partialdenom = n1-n2;
	
	
	denominator = n2 * partialdenom;
	
	

	answer = n1 / denominator;


	
	
	//factorial = (n / (k(n-k)));
	//	result = answer;


		cout<< "The factorial of  "<<n1 <<"!/"<< n2 <<"!("<< n1 << "!-" << n2 << "!) is "<<answer<< "."<<endl;
		cout << denominator << endl;
		cout << n1 << endl;
		cout << n2 << endl;
		cout << answer << endl;





	return 0;
}

any ideas on why this is happening?

thanks

If the problem is that answer is 0, it is likely that you are dividing everything away. Because int does not have a precision, anything past the radix will be truncated. 1/2 is 0.5 as double, but 0 as int because the .5 is cut off.

ok so i figured out where that was going wrong, thanks.

Now the code i wrote works but for some reason it is giving incorrect output at 14!... Any ideas why it would work for some numbers and not others?


thanks, code is below.

#include <iostream>
using namespace std;

int factorial (int);  //function prototype or declaration

int main()
{
	int n1, n2, result, n(0), k(0), partialdenom, numerator, denominator;
	int partialdenomfact, n2factorial;

	cout<<"Please enter two positive integer values less than 15. "<<endl;
	cin>>n1;
	cin>>n2;
	

	// varification that the numbers entered are less than 15 and greater than 1

	 while (n1 < 1 || n1 >15  || n2 < 1 || n2 > 15)
	{
		cout<<"You entered an incorrect integer value!"<<endl;
		cout<<"Please enter a positive integer value less than 15. "<<endl;
		cin>>n1;
		cin>>n2;
	}


	 // math operations
	numerator = factorial(n1);
	partialdenom = n1-n2;
	partialdenomfact = factorial(partialdenom);
	n2factorial = factorial(n2);
	denominator = partialdenomfact * n2factorial;
	result = numerator/denominator;


	// output

	cout<< "The factorial of  "<<n1 <<"!/"<< n2 <<"!("<< n1 << "-" << n2 << ")! is "<<result<< "."<<endl;
	cout << denominator << endl;
	cout << numerator << endl;
	cout << partialdenom << endl;
	cout << partialdenomfact << endl;
		





	return 0;
}

//Function to calculate and return the factorial of an integer
//Function requires an integer value from the function call as
//input and will return the integer factorial.
//Function utilizes a loop to calculate the factorial.
//Pre: a positive integer value has been entered and confirmed to
//     be greater less than 15
//Post: The calculated factorial is returned to the function call


int factorial(int n)
{
	int product = 1;
	for(; n > 1; n--)
	{
		product *= n;
}
	return product;
}

You are using integers to store the factorial of a number and obviously 13! 14! 15! will go out of integer range. It definitely overflows and that is the reason you are getting erroneous outputs. try changing the variables to long int or double and you will get the desired output.

ah, forgot about that, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.