I have written a program with 2 functions which compute factorials & combinations. The functions work perfectly when called but I cannot insert a for loop at the end which sums up the combos. This part works (I have omitted the functions because they work when called for).
long factorial(int n);
long combination(int n, int k);
int main ()
{
int n=0,k=0;
while(n<=0)
{
printf("Enter value for n:");
scanf("%i", &n);
if(n<=0)
printf("Please enter a positive integer value for n.\n");
}
while(k<=0||k>n)
{
printf("Enter value for k:");
scanf("%i", &k);
if(k<=0)
printf("Please enter a positive integer value for k.\n");
else if (k>n)
printf("k must be less than or equal to n.\n");
}
long f = factorial(n);
long c = combination(n,k);
printf("n! = %i\n", f);
printf("C(n,k) = %i\n", c);
system("pause");
return 0;
}
But when I insert this piece of code in the main body it goes haywire.
unsigned long sum=0;
int i;
for(i=0;i<=n;i++)
{
sum+=combination(n,i);
}
printf("sum=%un",sum);
What am I doing wrong? The sum of the combos should equal 2^n.
thanks!:eek: