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
Answered By
2 Likes
Related Questions
Write a Program in Java to input a number and check whether it is a Fascinating Number or not.
Fascinating Numbers: Some numbers of 3 digits or more exhibit a very interesting property. The property is such that, when the number is multiplied by 2 and 3, and both these products are concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless of the number of zeroes.
Let's understand the concept of Fascinating Number through the following example:
Consider the number 192
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results: 192 384 576
It could be observed that '192384576' consists of all digits from 1 to 9 exactly once. Hence, it could be concluded that 192 is a Fascinating Number. Some examples of fascinating Numbers are: 192, 219, 273, 327, 1902, 1920, 2019 etc.A number is said to Bouncy number if the digits of the number are unsorted.
For example,
22344 - It is not a Bouncy number because the digits are sorted in ascending order.
774410 - It is not a Bouncy number because the digits are sorted in descending order.
155349 - It is a Bouncy number because the digits are unsorted.
A number below 100 can never be a Bouncy number.Write a program in java to accept a number. Check and display whether it is a Bouncy number or not.
Write a Program in Java to input a number and check whether it is a Pronic Number or Heteromecic Number or not.
Pronic Number: A Pronic number, oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n (n + 1).
The first few Pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 … etc.Twins primes are consecutive prime numbers whose difference is 2.
For example, (3,5), (11,13), (17,19) are all twin primes. We define the distance of any twin prime pair from a positive integer as follows:
If (p1, p2) is a twin prime pair and n is a positive integer then the distance of the twin prime from n is: minimum(abs(n-p1), abs(n-p2)) where abs returns the absolute value of its argument, and minimum returns the smaller of its two arguments.
Write a program that reads in a positive integer n and prints out the twin prime pair that has the least distance from n.
For example:
(a) if n is 30 then the pair is (29, 31),
(b) if n is 13 it is (11,13), if n is 49 it is (41,43).
(c) if n is 54 it is (59, 61).Sample Input: 34
Sample Output:
Number read in is 34 p1= 29, p2=31Sample Input: 60
Sample Output:
Number read in is 60 p1= 59, p2=61