Java Program to Generate All Possible Combinations Out of a, b, c, d, e

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:

Hướng dẫn Java Design Pattern – Memento
Apache Commons Collections SetUtils
Java Program to Implement SynchronosQueue API
Java Program to Perform the Unique Factorization of a Given Number
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Implement Ternary Search Tree
The Modulo Operator in Java
Guide to Dynamic Tests in Junit 5
Spring Boot - Building RESTful Web Services
Java – Get Random Item/Element From a List
Từ khóa throw và throws trong Java
Java 14 Record Keyword
Spring MVC Setup with Kotlin
Sử dụng CountDownLatch trong Java
A Guide to Apache Commons Collections CollectionUtils
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Java Program to Generate Date Between Given Range
Java Program to add two large numbers using Linked List
Java Program to Implement Min Hash
Build a REST API with Spring and Java Config
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
The DAO with JPA and Spring
Removing Elements from Java Collections
Introduction to Spring Data MongoDB
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Concurrency Interview Questions and Answers
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
The Difference Between Collection.stream().forEach() and Collection.forEach()
Java Program to Implement Hash Tables with Quadratic Probing
Spring WebClient Requests with Parameters
Java Program to Implement ConcurrentLinkedQueue API
Java Program to Implement Merge Sort on n Numbers Without tail-recursion