# Calculate Binomial Coefficient

  • To calculate the Binomial Coefficient C(n,k).

# Source Code - C++

#include <iostream>
using namespace std;

//Function to calculate Binomial Coefficient
int binomialC (int n, int k)
{
 int ans=1;
 if(k>n-k)
 k=n-k;

 for(int i=0; i<k; i++)
{
  ans= ans*(n-i);
  ans= ans/(i+1);
 }
 return ans;
}
//Main function
int main()
{
 int n,k,result;
 //input C(n,k)
 cout<< "enter value of n & k \n";
 cin>>n>>k;
 //calling Binomial Coefficient Function
 result=binomialC(n,k);
 cout<<"Binomial coefficient is:"<<result;
 return 0;

}

Time Complexity : O(k)

Learn More


  • Binomial Coefficient