This is a java program to generate and print all possible combinations out of a, b, c, d, e. The trick here is to start with one letter combinations, then with two letter combinations and so on.
Here is the source code of the Java Program to Generate All Possible Combinations Out of a, b, c, d, e. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to print all possible combinations out of a, b, c, d, e
public class All_Possible_Combinatons
{
static void printCombinations(char[] sequence, int N)
{
char[] data = new char[N];
for (int r = 0; r < sequence.length; r++)
combinations(sequence, data, 0, N - 1, 0, r);
}
static void combinations(char[] sequence, char[] data, int start, int end,
int index, int r)
{
if (index == r)
{
for (int j = 0; j < r; j++)
System.out.print(data[j] + " ");
System.out.println();
}
for (int i = start; i <= end && ((end - i + 1) >= (r - index)); i++)
{
data[index] = sequence[i];
combinations(sequence, data, i + 1, end, index + 1, r);
}
}
public static void main(String args[])
{
char[] sequence = { 'a', 'b', 'c', 'd', 'e' };
System.out.print("The combinations are: ");
printCombinations(sequence, sequence.length);
}
}
Output:
$ javac All_Possible_Combinatons.java $ java All_Possible_Combinatons The combinations are: a b c d e a b a c a d a e b c b d b e c d c e d e a b c a b d a b e a c d a c e a d e b c d b c e b d e c d e a b c d a b c e a b d e a c d e b c d e
Related posts:
A Guide to Spring Boot Admin
Java Program to Perform Stooge Sort
Phương thức forEach() trong java 8
Converting a List to String in Java
Using the Map.Entry Java Class
“Stream has already been operated upon or closed” Exception in Java
Hướng dẫn sử dụng Printing Service trong Java
Java equals() and hashCode() Contracts
Java Program to Implement Leftist Heap
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Send email with SMTPS (eg. Google GMail)
Implementing a Runnable vs Extending a Thread
Java Program to Implement Johnson’s Algorithm
How to Convert List to Map in Java
Java Program to Perform Finite State Automaton based Search
Jackson vs Gson
What is Thread-Safety and How to Achieve it?
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Apache Commons Collections Bag
How to Iterate Over a Stream With Indices
Java Program to Create the Prufer Code for a Tree
Java Program to Implement Bresenham Line Algorithm
Finding the Differences Between Two Lists in Java
Number Formatting in Java
Spring @Primary Annotation
Quick Guide to the Java StringTokenizer
Automatic Property Expansion with Spring Boot
Merging Two Maps with Java 8
Java Program to Generate Random Numbers Using Middle Square Method
Lớp LinkedHashMap trong Java
Beans and Dependency Injection