Trying to finish my program for binomial coefficients. It compile but it keeps giving me 0 which is not correct. I think it may be a problem with my for loops but I can't figure it out. Here is the pseudocode:

/* pseudocode for Binomial
Coefficients */
int binomial(int n, int k)
{
If (n < k) Then return (0)
Else 
{
Set denominator = 1*2*...*k
Set numerator = (n-k+1)*(n-k+2)*...*(n-1)*n
return (numerator / denominator)
} // else
End if
}

This is what I have so far.

if (n < k)
{   
    return(0) ;
} 
else 
{  
    denominator = 1;  

      for (int i = 1; i <= k; i++) 
      {  denominator = denominator *i ;  }  
     numerator = 1;  
      for (int i = n - k + 1; i <= n ; i++) 
      {    numerator =  numerator *i ;  }  

return (numerator / denominator) ;
}

> It compile but it keeps giving me 0 which is not correct.
For what values of n and k?

> denominator = denominator *i ;
These would have numeric overflow pretty quickly.
Add some debug to print each denominator value in the loop.

Also show us how you print the result in the end - are you even assigning the result to a variable you then print?

This is my entire program thus far:

#include <iostream> 

using namespace std ; 

int binomial(int n, int k) ; // function prototype 


int main()
{ 
   int n, k ;   // parameters for the binomial number 
   int result ;   

   cout << endl ; 

   // read in n & k   

   cout << "Enter n (positive integer) :  " ;
   cin >> n ; 

   cout << "Enter k (positive integer) : " ;
   cin >> k ;

   result = binomial (n,k) ; 

   cout << "Binomial number " << n << "C" << k
        << " = " << result << endl ;

   return (0) ;
}

// ********************************************************

int binomial(int n, int k)

/* Computes the binomial coefficient nCk */
/* */
/* Inputs:                               */
/*    n, k (integers)                    */
/*                                       */
/* Output:                               */
/*    binomial coefficient nCk (integer) */

{
   int numerator, denominator ;
   int i ; // needed to compute numerator & denominator

   if (n<k ) 
   {
     return(0 ) ; 
   }
   else 
   {
      denominator = 1   ; 

      for (i = 1   ; i <= n   ; i = i+1)
        denominator =    *    ;
        
     numerator = 1
     for(i= n-k+1 ; i<= k  ; i = i+1)

      return (numerator / denominator  ) ; 
   } 
}

I have tried several different values for n and k but still get 0.

What you posted won't even compile.

Nor are we any clearer as to what numbers you tested with.
2 and 3 perhaps?
or 12345 and 67890

It compiles but no matter what I use for n and k I get 0. I tried using n=6 and k=1 and got 0. I tried using n=52 and k=25 and got 0.

It compiles but no matter what I use for n and k I get 0. I tried using n=6 and k=1 and got 0. I tried using n=52 and k=25 and got 0.

It doesn't compile. Look at line 56. Fix that, then look at the "loop" on line 59 ("loop" is in quotes on purpose. What code do you want to repeat?).

It doesn't compile. Look at line 56. Fix that, then look at the "loop" on line 59 ("loop" is in quotes on purpose. What code do you want to repeat?).

First off I forgot to include my actual line 1 which is include stdafx.h. For line 56 I have denominator= denominator * i. My loops are where I think the problem lies. I am unsure how to figure for the consecutive numbers.

commented: Unless you're doing copy/paste of your ACTUAL code, you're wasting everyones time -4

First off I forgot to include my actual line 1 which is include stdafx.h. For line 56 I have denominator= denominator * i. My loops are where I think the problem lies. I am unsure how to figure for the consecutive numbers.

Again, your lower loop isn't a "loop". You'll never go through it more than once since it immediatedly returns a value. The return value needs to be AFTER the loop is over. Add brackets to make it clearer.

Right now you have this (brackets added):

numerator = 1
     for(i= n-k+1 ; i<= k  ; i = i+1)
     {
          return (numerator / denominator  ) ; 
     }

I imagine you want this:

numerator = 1
     for(i= n-k+1 ; i<= k  ; i = i+1)
     {
          // do something
     }
     return (numerator / denominator  ) ;

Again, your lower loop isn't a "loop". You'll never go through it more than once since it immediatedly returns a value. The return value needs to be AFTER the loop is over. Add brackets to make it clearer.

Right now you have this (brackets added):

numerator = 1
     for(i= n-k+1 ; i<= k  ; i = i+1)
     {
          return (numerator / denominator  ) ; 
     }

I imagine you want this:

numerator = 1
     for(i= n-k+1 ; i<= k  ; i = i+1)
     {
          // do something
     }
     return (numerator / denominator  ) ;

I have inserted numerator= numerator * i between the brackets. Should I do the same thing for the denominator?

I have inserted numerator= numerator * i between the brackets. Should I do the same thing for the denominator?

You should figure out what you do in real life and do that in the code. "Do the same thing for the denominator" could mean several things. Multiply it times denominator *i? Multiply it by numerator * i? Use the same loop control values?

Start with a bunch of small pairs that you can computer easily. Make sure numerator and denominator come out right. Display their values for debugging purposes and make sure they are right. Make sure it works correctly from 0, 1, and 2, then go from there. By the time everything works perfectly up to 3 and 4, there's a damn good chance it'll work for 5 and up.

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.