Wednesday, February 25, 2015

Binomial Coefficients in a java pgm



BinomialCoefficients.java

public class BinomialCoefficients
{
    public static void main(String[] args)
{
        int N = Integer.parseInt(args[0]);

        int[][] pascal = new int[N+1][];

        pascal[1] = new int[1 + 2];
        pascal[1][1] = 1;

        for (int n = 2; n <= N; n++)
       {
            pascal[n] = new int[n + 2];
            for (int k = 1; k < pascal[n].length - 1; k++)
                pascal[n][k] = pascal[n-1][k-1] + pascal[n-1][k];
        }
        int denominator = 1;
        for (int n = 1; n <= N; n++)
        {
            for (int k = 1; k < pascal[n].length - 1; k++)
           {
                System.out.print(pascal[n][k] + "/" + denominator + " ");
            }
            System.out.println();
            denominator += denominator;
        }
    }
}

No comments:

Post a Comment