Java program to find permutation and combination (nCr nPr)

In this tutorial, we will see java program to find permutation and combination.
Permutation is represented as nPr and Combination is represented as nCr.It is simple program which involves factorial of the numbers.

Here is formula to find permutation and combination (nCr nPr).

nPr = factorial(n) / factorial(n-r)
nCr = factorial(n)/(factorial(n-r) * factorial(r))
import java.util.Scanner;

public class PermutationCombinationMain
{
    public static int fact(int num){
        int fact=1, i;
        for(i=1; i<=num; i++){
            fact = fact*i;
        }
        return fact;
    }
    public static void main(String args[]){
        int n, r;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter Value of n : ");
        n = scanner.nextInt();
        System.out.print("Enter Value of r : ");
        r = scanner.nextInt();

        System.out.print("NCR is " +(fact(n)/(fact(n-r)*fact(r))));
        System.out.print("\nNPR is " +(fact(n)/(fact(n-r))));
    }
}

Output:

Enter Value of n : 8
Enter Value of r : 4
NCR is 70
NPR is 1680
[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.