- Related Questions & Answers
- How to create a thread without implementing the Runnable interface in Java?
- How to read the data from a properties file in Java?
- How to instantiate a static inner class with reflection in Java?
- Will a finally block execute after a return statement in a method in Java?
- Importance of XOR operator in Java?
- How to convert an OutputStream to a Writer in Java?
- How to convert a String to an InputStream object in Java?
- How to find the unicode category for a given character in Java?
- How to print a formatted text using printf() method in Java?
- How to find the number of days in a month of a particular year in Java?
- Importance of deepToString() and asList() methods in Java?
- How to create a thread by using anonymous class in Java?
- How to remove the HTML tags from a given string in Java?
- How to find If a given String contains only letters in Java?
- While chaining, can we throw unchecked exception from a checked exception in java?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Permutation and Combination are a part of Combinatorics. Permutation is the different arrangements that a set of elements can make if the elements are taken one at a time, some at a time or all at a time. Combination is is the different ways of selecting elements if the elements are taken one at a time, some at a time or all at a time.

An example of this is given as follows −

Permutation = factorial(n) / factorial(n-r); Combination = factorial(n) / (factorial(r) * factorial(n-r)); n = 5 r = 3 Permutation = 60 Combination = 10

A program that demonstrates this is given as follows −

public class Example { static int factorial(int n) { int fact = 1; int i = 1; while(i <= n) { fact *= i; i++; } return fact; } public static void main(String args[]) { int n = 7, r = 3, comb, per; per = factorial(n) / factorial(n-r); System.out.println("Permutation: " + per); comb = factorial(n) / (factorial(r) * factorial(n-r)); System.out.println("Combination: " + comb); } }

The output of the above program is as follows −

Permutation: 210 Combination: 35

Now let us understand the above program.

The function factorial finds the factorial of the number n using a while loop. Then it returns fact. The code snippet that demonstrates this is given as follows −

static int factorial(int n) { int fact = 1; int i = 1; while(i <= n) { fact *= i; i++; } return fact; }

In the function main(), the permutation and combination of n and r are found using their respective formulas. Then the results are displayed. The code snippet that demonstrates this is given as follows −

public static void main(String args[]) { int n = 7, r = 3, comb, per; per = factorial(n) / factorial(n-r); System.out.println("Permutation: " + per); comb = factorial(n) / (factorial(r) * factorial(n-r)); System.out.println("Combination: " + comb); }

Advertisements