Find nCr and nPr

import java.util.*;
class Factorial1
{
    public static void main(String args[])
    {
        int n,r,nCr,nPr;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the values of n and r  to find nCr :");
        n=sc.nextInt();
        r=sc.nextInt();
        nCr=facto(n)/(facto(r)*facto(n-r));
        System.out.println("nCr="+nCr);
        System.out.println("Enter the values of n and r to find nPr :");
        n=sc.nextInt();
        r=sc.nextInt();
        nPr=facto(n)/facto(n-r);
        System.out.println("nPr="+nPr);

    }
    static int facto (int n)
    {
        int t,fact=1;
        for(i=1;i<=n;i++)
        {
            fact=fact*i;
        }
        return fact;
    }
}




No comments :

Post a Comment