-1

i have the following code that outputs the binomial coefficient of 2 numbers. I would like to include the two numbers in a statement that is printed out along with the overall result but i am receiving the following error:

_ cannot be resolved to a variable

here is my code:

public class BinomialCoefficients
{
    private static long binomial(int n, int k)
    {
        if (k>n-k)
            k=n-k;

        long b=1;
        for (int i=1, m=n; i<=k; i++, m--)
            b=b*m/i;
        return b;
    }

    public static void main(String[] args)
    {
        System.out.println("The Binomial Coefficients of" + n + "and " + k + " is: " + binomial(15, 4));
    }
}

any help?

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
nalydddd
  • 21
  • 4

2 Answers2

0

You don't define variables n and k in your main method.

Declare variables

int n = 15;
int k = 4;

Then also use these in your call to the binomial method, rather than hard-coding 15 and 4:

System.out.println("The Binomial Coefficients of" + n + "and " + k + " is: " + binomial(n, k));
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

I suppose you are getting this compilation error in your mainmethod. You have to declare the variables k and n before using them in main.

You can update your code as follows:

public class BinomialCoefficients
{
    private static long binomial(int n, int k)
    {
        if (k>n-k)
            k=n-k;

        long b=1;
        for (int i=1, m=n; i<=k; i++, m--)
            b=b*m/i;
        return b;
    }

    public static void main(String[] args)
    {
        int n = 15;
        int k = 4;
        System.out.println("The Binomial Coefficients of" + n + "and " + k + " is: " + binomial(n, k));
    }
}
gjigandi
  • 486
  • 3
  • 7