permutation & combination: code crashes with large numbers

This code works for small number combinations but crashes when I tried larger numbers, what can I do to solve the problem?

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
31
32
33
34
35
36
37
38
39
40
41
42

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int factorial(int num)
{
	int n = 1;
	if (num<0)
	{
		cerr<<"Error, no factorial for a negative number.\n";
		return 0;
	}
	while (num>0)
	{
		n *= num;
		num--; 
	}
	return n;
}

int main(int nNumberofArgs, char* pszArgs[])
{
	cout<<"This program finds 10C5, 100C10 and 40C30.\n";
	int combination;
	int n1 = 10; int r1 = 5; int n2 = 100;
	int r2 = 10; int n3 = 40; int r3 = 30;
	//nCr = n!/(r!(n-r)!)
	combination = factorial(n1)/((factorial(r1))*factorial(n1-r1));
	cout<<"10C5 = "<<combination<<endl;
	combination = factorial(n2)/((factorial(r2))*factorial(n2-r2));
	cout<<"100C10 = "<<combination<<endl;
	combination = factorial(n3)/((factorial(r3))*factorial(n3-r3));
	cout<<"40C30 = "<<combination<<endl;
	//wait for the user to be ready before terminating the program
	cin.get();
	return 0;
}
Topic archived. No new replies allowed.