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:
Java Program to Implement Skip List
Uploading MultipartFile with Spring RestTemplate
Java Program to Perform Finite State Automaton based Search
Iterating over Enum Values in Java
Spring Boot with Multiple SQL Import Files
Hướng dẫn sử dụng String Format trong Java
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Retrieve User Information in Spring Security
Java Program to Represent Graph Using Incidence List
So sánh Array và ArrayList trong Java
Java Program to Implement Find all Cross Edges in a Graph
Calling Stored Procedures from Spring Data JPA Repositories
Versioning a REST API
Zipping Collections in Java
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Các kiểu dữ liệu trong java
Spring MVC Tutorial
Deploy a Spring Boot App to Azure
Working With Maps Using Streams
Fixing 401s with CORS Preflights and Spring Security
Programmatic Transaction Management in Spring
Java Program to Implement PriorityQueue API
Guide to Character Encoding
Using the Not Operator in If Conditions in Java
Refactoring Design Pattern với tính năng mới trong Java 8
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to Emulate N Dice Roller
Custom Thread Pools In Java 8 Parallel Streams
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement IdentityHashMap API
Java Program to Implement a Binary Search Tree using Linked Lists