// Md Al Masum Bhuiyan /*This code is for finding the value of (x + y )^n using the binomial coeffecient (for any positive integer n) */ #include #include using namespace std ; int facto (int num ) // factorial function { int i =1, fact; fact = i; while(i<=num) { fact=fact*i; // the definition of factorial i++; } return fact ; } int binom (int num, int num1) // Binomial function { float bin; int x = facto(num); // calling factorial function int y = facto(num1); int z = facto(num-num1); bin= x/(y * z); // The Binomial coeffecient return bin; } int main() { int n,i; double x,y; double sum =0; double rslt = 0; cout << "Enter the number of n : " << endl; cin >> n; cout << "Enter the number of x : " << endl; cin >> x; cout << "Enter the number of y : " << endl; cin >> y; while (( x == 0 && y == 0 && n == 0 ) || (n < 0)) // invalid input { cout << "Invalid input. Please try again. \n "; cout << "Enter the number of n : " << endl; cin >> n; cout << "Enter the number of x : " << endl; cin >> x; cout << "Enter the number of y : " << endl; cin >> y; } for (i=0; i<=n; i++) { rslt = binom(n,i) * pow(x,(n-i)) * pow(y,i); // The Binomial theorem sum = sum + rslt; } cout << " By the binomial theorem, the result of (x+y)^n is : " << sum << endl; // Final result return 0; }