Skip to content

Instantly share code, notes, and snippets.

@prodhan
Created January 22, 2019 21:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save prodhan/5200af38fbc1044eb9ee734a36182ba7 to your computer and use it in GitHub Desktop.
Binomial theorem in c++ with another method
/*
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 N\n";
cin>>n;
sum=pow(x,n);
for(r=0; r<=n; r++) {
//Method 1
if(r==0){
cout<<"X^"<<n<<"+";
continue;
}
if(r==n){
cout<<"Y^"<<n;
continue;
}
cout<<n<<"C"<<r<<"X^(n-"<<r<<")Y^"<<r<<"+";
// Method 2
// cout<<n<<"C"<<r<<"X^(n-"<<r<<")Y^"<<r;
// if(r<n)
// cout<<"+";
}
cout<<"\nGive the value of X\n";
cin>>x;
cout<<"Give the value of Y\n";
cin>>y;
sum=pow(x+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