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 – Write a Reader to File
Primitive Type Streams in Java 8
Guide to Mustache with Spring Boot
Spring Boot - Internationalization
How to Round a Number to N Decimal Places in Java
Java Program to Implement Binomial Heap
Interface trong Java 8 – Default method và Static method
Java 8 Stream API Analogies in Kotlin
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Solve a Matching Problem for a Given Specific Case
Spring Boot - Runners
Spring MVC + Thymeleaf 3.0: New Features
Java Program to Perform Insertion in a BST
Exploring the Spring 5 WebFlux URL Matching
Spring MVC Custom Validation
Java Program to Implement LinkedTransferQueue API
Introduction to the Functional Web Framework in Spring 5
Introduction to Spring MVC HandlerInterceptor
HashSet trong Java hoạt động như thế nào?
A Quick JUnit vs TestNG Comparison
Deploy a Spring Boot App to Azure
Java Program to Implement Insertion Sort
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Remove the First Element from a List
Spring MVC Async vs Spring WebFlux
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Form Validation with AngularJS and Spring MVC
Spring Cloud Bus
Spring REST API + OAuth2 + Angular
Java Program to Implement Sorted Circularly Singly Linked List
Hướng dẫn Java Design Pattern – Prototype
Java Program to Optimize Wire Length in Electrical Circuit