Java Program to Compute Combinations using Recurrence Relation for nCr

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.

  1. //This is a java program to print all possible combinations out of a, b, c, d, e
  2.  
  3. public class All_Possible_Combinatons 
  4. {
  5.     static void printCombinations(char[] sequence, int N) 
  6.     {
  7.         char[] data = new char[N];
  8.         for (int r = 0; r < sequence.length; r++)
  9.             combinations(sequence, data, 0, N - 1, 0, r);
  10.     }
  11.  
  12.     static void combinations(char[] sequence, char[] data, int start, int end,
  13.             int index, int r) 
  14.     {
  15.  
  16.         if (index == r) 
  17.         {
  18.             for (int j = 0; j < r; j++)
  19.                 System.out.print(data[j] + " ");
  20.             System.out.println();
  21.         }
  22.  
  23.         for (int i = start; i <= end && ((end - i + 1) >= (r - index)); i++) 
  24.         {
  25.             data[index] = sequence[i];
  26.             combinations(sequence, data, i + 1, end, index + 1, r);
  27.         }
  28.     }
  29.  
  30.     public static void main(String args[]) 
  31.     {
  32.         char[] sequence = { 'a', 'b', 'c', 'd', 'e' };
  33.         System.out.print("The combinations are: ");
  34.         printCombinations(sequence, sequence.length);
  35.     }
  36. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.