Printing All Possible Combinations of a 3 Digit Number Java Program

Printing All Possible Combinations of a 3 Digit Number Java Program:

Logic:

  • We have to write 3 for loops and need to ensure all three for loops variable does not have same value.
  • Then we can take and print the input array with the three for loop variables to get all different possibles.

Better Complexity Solution is here:

Printing All Possible Combinations

Java Program to print All possible combinations of a 3 Digit number/character:

package com.ngdeveloper;
public class PossibleCombinations {
	public static void main(String[] args) {
		int[] input = { 1, 4, 3 };
		for (int x = 0; x < 3; x++) {
			for (int y = 0; y < 3; y++) {
				for (int z = 0; z < 3; z++) {
					if (x != y && y != z && z != x) {
						System.out.println(input[x] + "" + input[y] + "" + input[z]);
					}
				}
			}
		}
	}
}

Output:

143
134
413
431
314
341

Leave a Reply