Write a program that computes C(n, k), i.e. the number of k-element subsets of a set with n elements. Remember that C(n, k) = n!/(k! (n-k)!) . Your program should ask the user to enter n and k, and compute and print C(n, k).

Write a program that computes C(n, k), i.e. the number of k-element subsets of a set with n elements. Remember that C(n, k) = n!/(k! (n-k)!) . Your program should ask the user to enter n and k, and compute and print C(n, k). 


PROGRAM :


import java.util.Scanner;

public class factorial

{

static int fact(int x)

{

int r=1,count;

for(count=1;count<=x;count++)

r=r*count;

return r;

}

public static void main(String args[])

{

Scanner input=new Scanner(System.in);

int a,b,c;

System.out.println("enter the 2 no.s");

int n=input.nextInt();

int k=input.nextInt();

if(n<0 || k<0)

System.out.println("number should be non-negative");

else

{

a=fact(n);

b=fact(k);

c=fact(n-k);

System.out.println("result of c(n,k) is=" +a/(b*c));

}

input.close();

}

}


Output:

enter the 2 no.s

2

2

result of c(n, k) is=1


Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Abstract classes and Abstract methods in Java with Examples rcub.

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.