# Java Program to Generate All Possible Combinations of List of Numbers

This is a java program to generate and print all the permutation of the Numbers. User first enters the element in the set and then actual elements. The notion of permutation relates to the act of permuting, or rearranging, members of a set into a particular sequence or order (unlike combinations, which are selections that disregard order). For example, there are six permutations of the set {1,2,3}, namely (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1).

Here is the source code of the Java Program to Generate All Possible Combinations of a Given List of Numbers. 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 perform all permutation of given list of numbers of a specific length`
2. `import java.util.Random;`
3. `import java.util.Scanner;`
4. ` `
5. `public class Permute_All_List_Numbers `
6. `{`
7. `    static void permute(int[] a, int k) `
8. `    {`
9. `        if (k == a.length) `
10. `        {`
11. `            for (int i = 0; i < a.length; i++) `
12. `            {`
13. `                System.out.print(" [" + a[i] + "] ");`
14. `            }`
15. `            System.out.println();`
16. `        } `
17. `        else `
18. `        {`
19. `            for (int i = k; i < a.length; i++) `
20. `            {`
21. `                int temp = a[k];`
22. `                a[k] = a[i];`
23. `                a[i] = temp;`
24. ` `
25. `                permute(a, k + 1);`
26. ` `
27. `                temp = a[k];`
28. `                a[k] = a[i];`
29. `                a[i] = temp;`
30. `            }`
31. `        }`
32. `    }`
33. ` `
34. `    public static void main(String args[]) `
35. `    {`
36. `        Random random = new Random();`
37. `        Scanner sc = new Scanner(System.in);`
38. `        System.out.println("Enter the length of list: ");`
39. `        int N = sc.nextInt();`
40. `        int[] sequence = new int[N];`
41. ` `
42. `        for (int i = 0; i < N; i++)`
43. `            sequence[i] = Math.abs(random.nextInt(100));`
44. ` `
45. `        System.out.println("The original sequence is: ");`
46. `        for (int i = 0; i < N; i++)`
47. `            System.out.print(sequence[i] + " ");`
48. ` `
49. `        System.out.println("\nThe permuted sequences are: ");`
50. `        permute(sequence, 0);`
51. ` `
52. `        sc.close();`
53. `    }`
54. `}`

Output:

```\$ java Permute_All_List_Numbers.java
\$ java Permute_All_List_Numbers

Enter the length of list:
3
The original sequence is:
15 61 16
The permuted sequences are:
[15]  [61]  [16]
[15]  [16]  [61]
[61]  [15]  [16]
[61]  [16]  [15]
[16]  [61]  [15]
[16]  [15]  [61]

Enter the length of list:
4
The original sequence is:
50 98 4 61
The permuted sequences are:
[50]  [98]  [4]  [61]
[50]  [98]  [61]  [4]
[50]  [4]  [98]  [61]
[50]  [4]  [61]  [98]
[50]  [61]  [4]  [98]
[50]  [61]  [98]  [4]
[98]  [50]  [4]  [61]
[98]  [50]  [61]  [4]
[98]  [4]  [50]  [61]
[98]  [4]  [61]  [50]
[98]  [61]  [4]  [50]
[98]  [61]  [50]  [4]
[4]  [98]  [50]  [61]
[4]  [98]  [61]  [50]
[4]  [50]  [98]  [61]
[4]  [50]  [61]  [98]
[4]  [61]  [50]  [98]
[4]  [61]  [98]  [50]
[61]  [98]  [4]  [50]
[61]  [98]  [50]  [4]
[61]  [4]  [98]  [50]
[61]  [4]  [50]  [98]
[61]  [50]  [4]  [98]
[61]  [50]  [98]  [4]```

Sanfoundry Global Education & Learning Series – 1000 Java Programs.