Sum of squares of binomial coefficients in C++

C++Server Side ProgrammingProgramming

The binomial coefficient is a quotation found in a binary theorem which can be arranged in a form of pascal triangle it is a combination of numbers which is equal to nCr where r is selected from a set of n items which shows the following formula

nCr=n! / r!(n-r)!
or
nCr=n(n-1)(n-2).....(n-r+1) / r!

The sum of the square of Binomial Coefficient i.e (nC0)2 + (nC1)2 + (nC2)2 + (nC3)2 + ……… + (nCn-2)2 + (nCn-1)2 + (nCn)2

Input :n=5
Output:252

Explanation

In this program first we have to find the binomial coefficient of r which is selected from n set then we have to square each coefficient and sum them we can derive a formula from the above equation or use of factorial function of each digit to get a sum so we will fall or factorial function in which we will pass and r for a given equation and then add it then we get the solution

Example

 Live Demo

#include <iostream>
using namespace std;
int fact(int n){
   int fact = 1, i;
   for (i = 2; i <= n; i++){
      fact *= i;
   }
   return fact;
}
int main(){
   int n=5;
   int sum = 0;
   int temp=0;
   for (int r = 0; r <= n; r++){
      temp = fact(n)/(fact(r)*fact(n-r));
      sum +=(temp*temp);
   }
   cout<<sum;
   return 0;
}

Output

252
raja
Published on 24-Oct-2019 14:35:24
Advertisements