KnowledgeBoat Logo

Computer Science

Permutation and combination of two numbers 'n' and 'r' are calculated as:

nPr = !n / !(n - r)

nCr = !n / (!(n - r) * !r)

where, permutation is denoted as nPr and combination is denoted as nCr. The nPr means permutation of 'n' and 'r' & nCr means combination of 'n' and 'r'.

Write a program to calculate and display the number of permutation and combinations of two numbers 'n' and 'r' by using the above formula.

Sample Input:
Enter the value of n: 11
Enter the value of r: 10

Sample Output:
nPr is : 39916800
nCr is : 11

Java

Java Iterative Stmts

2 Likes

Answer

import java.util.Scanner;

public class KboatPermutationCombination
{
    static long factorial(int num) {
        int f = 1;
        for (int i = 1; i <= num; i++) {
            f *= i;
        }
        return f;
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = in.nextInt();
        System.out.print("Enter the value of r: ");
        int r = in.nextInt();
        int p = (int)(factorial(n) / factorial(n - r));
        int c = (int)(factorial(n) 
                      / (factorial(n - r) * factorial(r)));
        System.out.println("Permutation = " + p);
        System.out.println("Combination = " + c);
    }
}

Output

BlueJ output of Permutation and combination of two numbers 'n' and 'r' are calculated as: n P r = !n / !(n - r) n C r = !n / (!(n - r) * !r) where, permutation is denoted as n P r and combination is denoted as n C r . The n P r means permutation of 'n' and 'r' & n C r means combination of 'n' and 'r'. Write a program to calculate and display the number of permutation and combinations of two numbers 'n' and 'r' by using the above formula. Sample Input: Enter the value of n: 11 Enter the value of r: 10 Sample Output: n P r is : 39916800 n C r is : 11

Answered By

2 Likes


Related Questions