Friday 25 October 2013

Combination using Recursion

import java.io.*;
class Combination
{
int n,k;
Combination()
{
n=0;k=0;
    }
    void read()throws IOException
    {
    BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter n=");
    n=Integer.parseInt(x.readLine());
    System.out.println("Enter k=");
    k=Integer.parseInt(x.readLine());
    }
    int fact(int i)
    {
    if(i<1)
    return 1;
    else
    return fact(i-1)*i;
    }
    void compute()
    {
    int c=fact(n)/(fact(k)*fact(n-k));
    System.out.println("Combination(n,k)="+c);
    }
    void display()
    {
    System.out.println("n="+n+" k="+k);
    compute();
    }
    public static void main (String[] args)throws IOException
    {
    Combination call=new Combination();
    call.read();
    call.display();
    }
}

No comments:

Post a Comment