Skip to content

Instantly share code, notes, and snippets.

@prodhan
Created January 14, 2019 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prodhan/49d1db7d5ffaf26231167f65c819e6a0 to your computer and use it in GitHub Desktop.
Save prodhan/49d1db7d5ffaf26231167f65c819e6a0 to your computer and use it in GitHub Desktop.
Binomial theorem in c++
/*
Created By Ariful Islam
Batch E-64 (DIU)
Roll: 34
*/
#include<iostream>
#include <math.h>
using namespace std;
int factorial(int a)
{
if(a<=1)
{
return 1;
}
else
{
return a*factorial(a-1);
}
}
int nCr(int p, int q)
{
return factorial(p)/(factorial(p-q)*factorial(q));
}
int main()
{
int x,y,n,r;
double sum=0, temp=0;
cout<<"Give the value of X\n";
cin>>x;
cout<<"Give the value of Y\n";
cin>>y;
cout<<"Give the value of N\n";
cin>>n;
sum=pow(x,n);
r=1;
for(r=1; r<=n; r++) {
temp=(nCr(n, r))*(pow(x, (n-r)))*(pow(y, r));
sum=sum+temp;
}
// sum=sum+pow(y,n);
cout<<"Result : "<<sum;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment