Program to calculate permutation and combination

Program:
import java.util.*;
class Pattern
{
public static void main (String[] args)
{

int nCr,nPr,n,r;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of n and r");
n=sc.nextInt();
r=sc.nextInt();
nPr=fact(n)/fact(n-r);
nCr=fact(n)/(fact(r)*fact(n-r));
System.out.println("nCr= "+nCr);
System.out.println("nPr= "+nPr);
}
static int fact(int n)
{
int f=1;
if(n==0)
return 1;
else
{
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
}
}
Output:


Comments

Popular posts from this blog

Sequential File Allocation

Indexed File Allocation method

Linked File Allocation