Yash
2012-10-17 17:07:51 UTC
I need to write a program with a method named "choose" to compute the binomial coefficient c(n,k) after the user enters n & k. I then need to have second part of the program where the program asks the user to input an integer n and then the program prints the first n rows of Pascal's triangle. On the ith row, the jth number is C(i, j). Both i and j start from 0. I am having a lot of trouble with this program so if somebody could show me how to do this it would be much appreciated! Here is what I have so far...i know it's not correct though.
import java.util.Scanner;
public class choose2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter values for n and k: ");
double i = input.nextInt();
double j = input.nextInt();
System.out.print("The binomial coefficient is: " + choose(i, j));
}
public static double choose(double n, double k) {
double dividend = 1;
double divisor = 1;
int x = 1;
x = x + 1;
for (n = 0; n <= (n-(k-1)); n++)
dividend = n * (n-x);
for (k = 0; k == 1; k++)
divisor = k * (k - x);
return dividend/divisor;
}
}