Problem:
The binomial theorem of basic algebra indicates that the coefficient nCr of the rth power of x in the expansion of (x+1)power n is given by
nCr = n!/r!(n-r)!
Design an algorithm that evaluates all coefficients of x for a given value of n.
Solution:
package com.myprograms;
import java.util.Scanner;
public class BinomialTheorem {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter n value");
int n = s.nextInt();
BinomialTheorem binomialTheorem = new BinomialTheorem();
int nFactorial = binomialTheorem.findFactorial(n);
System.out.println("enter r value");
int r = s.nextInt();
int rFactorial = binomialTheorem.findFactorial(r);
int nrFactorial = binomialTheorem.findFactorial(n-r);
int ncrValue = nFactorial/(rFactorial * nrFactorial);
System.out.println("the ncr values is: " + ncrValue);
s.close();
}
private int findFactorial(int n){
int product = 1;
for(int i = 1; i<= n; i++){
product = product * i;
}
return product;
}
}
Output:
enter n value
7
enter r value
3
the ncr values is: 35
The binomial theorem of basic algebra indicates that the coefficient nCr of the rth power of x in the expansion of (x+1)power n is given by
nCr = n!/r!(n-r)!
Design an algorithm that evaluates all coefficients of x for a given value of n.
Solution:
package com.myprograms;
import java.util.Scanner;
public class BinomialTheorem {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter n value");
int n = s.nextInt();
BinomialTheorem binomialTheorem = new BinomialTheorem();
int nFactorial = binomialTheorem.findFactorial(n);
System.out.println("enter r value");
int r = s.nextInt();
int rFactorial = binomialTheorem.findFactorial(r);
int nrFactorial = binomialTheorem.findFactorial(n-r);
int ncrValue = nFactorial/(rFactorial * nrFactorial);
System.out.println("the ncr values is: " + ncrValue);
s.close();
}
private int findFactorial(int n){
int product = 1;
for(int i = 1; i<= n; i++){
product = product * i;
}
return product;
}
}
Output:
enter n value
7
enter r value
3
the ncr values is: 35
No comments:
Post a Comment