You are here:

# C++/C++ code for Binomial Coefficient

Question
#include <iostream>
using namespace std;

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

int _tmain()
{
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) then
{
return(0) ;
}
else
{
denominator = k ; // initial value

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

return (n) ;
} // else if
} // end function binomial

I am trying to write the code for the if then statement and anything else that you can help with

Hi Christopher

There is no "then" in the C "if" statement.
It's simply written as
if (n<k)
{
return(0) ;
}

The formula for nCk can be written with factorials as
factorial(n) / (factorial(k) * factorial(n-k))
so try writing a factorial function and have the binomial function use the factorial function.

Best regards
Zlatko

C++

Volunteer

#### Zlatko

##### Expertise

No longer taking questions.

##### Experience

No longer taking questions.

Education/Credentials
No longer taking questions.