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:
Changing Annotation Parameters At Runtime
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Guide to @JsonFormat in Jackson
Java Program to Represent Graph Using Adjacency Matrix
The Thread.join() Method in Java
Java Program to Perform Searching in a 2-Dimension K-D Tree
Java Program to Perform the Sorting Using Counting Sort
String Operations with Java Streams
Introduction to Eclipse Collections
A Guide to System.exit()
Introduction to Spring Cloud CLI
Spring Security 5 for Reactive Applications
Toán tử trong java
Hướng dẫn Java Design Pattern – Visitor
Java Program to Implement Flood Fill Algorithm
Java Program to Implement Nth Root Algorithm
Compact Strings in Java 9
Introduction to the Java NIO Selector
Java Program to Perform Right Rotation on a Binary Search Tree
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Prevent Brute Force Authentication Attempts with Spring Security
Java Program to Implement Horner Algorithm
Rate Limiting in Spring Cloud Netflix Zuul
Function trong Java 8
String Joiner trong Java 8
Convert XML to JSON Using Jackson
Using Java Assertions
Java Program to Generate Randomized Sequence of Given Range of Numbers
Lớp LinkedHashMap trong Java
Java Program to Implement Dijkstra’s Algorithm using Set
Beans and Dependency Injection
Exception Handling in Java