186k views
2 votes
Imagine you are playing a board game. you roll a 6-faced dice and move forward the same number of spaces that you rolled. if the finishing point is "n" spaces away from the starting point, please implement a program that calculates how many possible ways there are to arrive exactly at the finishing point. java

User Anunaki
by
7.6k points

1 Answer

4 votes

Answer:

/* Program in Java for finding possible ways of arriving exactly at the finishing point as mentioned above*/

import java.io.*;

import java.util.Scanner;

class Main

{

public static int fact(int num)

{

int i,m,fact=1;

int number;

System.out.println("Enter n:");

Scanner sm= new Scanner(System.in);

number= sm.nextInt();

for(i=1;i<=number;i++)

{

fact=fact*i;

}

return fact;

}

public static void main(String args[])

{

int m;

System.out.println("Enter the number of times dice is rolled:");

Scanner sn = new Scanner(System.in);

m= sn.nextInt();

int numofpossibleways= 126*fact(m)/fact(m-6);

System.out.print("Number of possible ways:"+ numofpossibleways);

}

}

Step-by-step explanation:

Suppose

starting point=0

spaces traveled=n

suppose there are m number of times dice is rolled.

hence for 6,5,4,3,2,1 it will be m(m-1)(m-2)(m-3)(m-4)(m-5)

So total ways to reach n spaces away from starting point:

(6* 6C1 +5*6C1+ 4*6C1+3*6C1+2*6C1+!*6C1)m!/(m-6)!

=((6+5+4+3+2+1)*6C1))m!/(m-6)!=(21*6!/1!5!)*m!/(m-6!)

=21*6= 126m!/'(m-6)!

User JustGettinStarted
by
8.1k points

No related questions found