With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Hi,

Write a Java program to print all possible combinations in below case

"n"(n>0) number of dices with "m"(m>0) sides. Print all possible combinations

for example.

2 dices with 3 sides should print out this

(1,1)

(1,2)

(1,3)

(2,1)

(2,2)

(2,3)

(3,1)

(3,2)

(3,3)

I gave it a sincere try before posting this. My code doesn't work if number of dices is more than 2 which is not right. Thanks in advance for looking.

Write a Java program to print all possible combinations in below case

"n"(n>0) number of dices with "m"(m>0) sides. Print all possible combinations

for example.

2 dices with 3 sides should print out this

(1,1)

(1,2)

(1,3)

(2,1)

(2,2)

(2,3)

(3,1)

(3,2)

(3,3)

I gave it a sincere try before posting this. My code doesn't work if number of dices is more than 2 which is not right. Thanks in advance for looking.

Also, I assume that this is for a homework/assignment exercise, but can you please confirm if that is the case? This allows everyone to provide the correct level of assistance.

```
import java.util.Arrays;
public class DiceHistogram {
public void getHistogram(int dices, int sides) {
int diceArray[] = new int[dices];
//Initialize Array
for (int i = 0; i< diceArray.length; i++) {
diceArray[i] = 1;
}
//System.out.println(Arrays.toString(diceArray));
for(int k=dices-2; k>=0; k--) {
for(int j=1; j<= sides; j++) {
diceArray[k] = j;
for(int i=1; i<=sides; i++ ) {
diceArray[dices-1] = i;
System.out.println(Arrays.toString(diceArray));
}
}
}
}
//for ( int j)
public static void main(String[] args) {
DiceHistogram dh = new DiceHistogram();
dh.getHistogram(2, 3);
}
}
```

I tried your program with different inputs and it works as expected. I am still trying to understand. will get back to you. Thanks for your help.

Hi Ozo,

Yes I think it's fair assumption.

static void getHistogram(int dices, int sides) {

for( int i=(int)Math.pow(sides,dice

System.out.print("(");

int n=i;

for( int d=0;d<dices;++d ){

if( d>0 ){ System.out.print(","); }

System.out.print(sides-(n%

n/=sides;

}

System.out.println(")");

}

}

http://www.mathsisfun.com/combinatorics/combinations-permutations.html

Here is my version. What you call a side, I call a face.

```
import java.util.*;
public class Dice{
public static void main(String[] args) {
int numberOfDice = 3;
int numberOfFaces = 3;
int numberOfOutcomes = (int)Math.pow(numberOfFaces,numberOfDice);
int[] roll = new int[numberOfOutcomes]; //all initially 0 (roll to the next face)
StringBuilder[] outcomes = new StringBuilder[numberOfOutcomes];
for(int j=0; j<numberOfOutcomes; j++){
outcomes[j] = new StringBuilder("(");
}
for(int k = 1; k <= numberOfDice; k++){
int numberOnFace = 1;
for(int y = 0; y < numberOfOutcomes; y++){
if(roll[y] == 0){
outcomes[y].append(numberOnFace);
if(k != numberOfDice)outcomes[y].append(",");
numberOnFace++;
roll[y] = 1; // don't roll, just stay on the same face
if(numberOnFace == numberOfFaces + 1){
numberOnFace = 1;
roll[y] = 0; // roll to the next face
}
}else{
outcomes[y].append(numberOnFace);
if(k != numberOfDice)outcomes[y].append(",");
}
}
}
for(int z = 0; z < numberOfOutcomes; z++){
System.out.println(outcomes[z].toString() + ")");
}
}
}
```

I am still trying to understand. will get back to you. Thanks for your help.

The key for a recursive solution is to understand that the larger problem can be seen as a repetition of a smaller problem.

E.g. If I gave you a list of all combinations of 2 dice with 3 sides:

(1,1)

(1,2)

(1,3)

(2,1)

(2,2)

(2,3)

(3,1)

(3,2)

(3,3)

then you can use this list to create the list of all combinations of 3 dice by adding "1, ...", "2, ..." and "3, ..." to the front of each of the earlier combinations:

1 + (1,1) => (1,1,1)

1+ (1,2) => (1,1,2)

1 + (1,3) => (1,1,3)

1 + (2,1) => (1,2,1)

...

3 + (1,1) => (3,1,1)

3 + (1,2) => (3,1,2)

...

etc..

Once you recognize the problem has that property, then you can write a function that calls to itself to get it to compute this.

Anyway - hope you can figure it out :)

Doug

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

That sort of problem is usually easiest to solve with recursion - where a function calls to itself.

Something like this.

Recursive functions tend to be not a lot of code, but can be hard to understand if you've not seen them before. I added some comments to help explain what's going on.

Doug

Open in new window