Computer Science, asked by Anonymous, 4 months ago


\Huge\sf\underline\red{Question}



WAP to roll a dice until a six comes up. Display the outcomes except when a one comes up. Also, display the number of iterations it takes.

Note☞ I want program + explanation!✓​

Answers

Answered by anindyaadhikari13
4

Required Answer:-

Question:

  • WAP to roll a dice until a six comes up. Display the outcomes except when a one comes up. Also, display the number of iterations it takes.

Solution:

Here comes the program.

public class Dice {

 public static void main(String[] args) {

    int count=0;

    while(true) {

       int d=(int)(Math.random()*6)+1;  

       if(d!=1)  {

          System.out.println("Dice rolling.. ");

          System.out.println("Result: "+d+"\n");

          ++count;

          }

       if(d==6)

         break;

    }

    System.out.println("Total Iterations:  "+count);

 }

}

Algorithm:

  1. Iterate a loop infinite times.
  2. Using random function, generate random numbers between 1 to 6. Consider that they are the outcomes of a dice.
  3. Count the number of iterations taken. Display the outcomes if the result is not 1 or else skip.
  4. Terminate the loop if the outcome is six.
  5. Display the total number of iterations taken.

Note: You may get different output when the given códe is executed as random function is used.

Refer to the attachment for output ☑.

Attachments:
Answered by Anonymous
1

Answer:

Refer to the attachment

Explanation:

Hope it helps

Attachments:
Similar questions