Search code examples
mathinteger-division

Math: Division Problems Generator


I am making a program that takes asks if you want to do Addition, Subtraction, Multiplication, and Division. It then asks how many questions. I have everything working but Division. I want to make sure that there will be no remainder when doing the division problem. I don't know how to make this work.

      else if (player==4) {

      System.out.print("How many questions would you like?-->"); player =
      in.nextInt(); numQuestions= player;


      do{

      //question
          do {
              do{
          num1 = (int) (Math.random() * 100);
          num2 = (int) (Math.random() *10);

              }while (num2 > num1);
          } while (num1 % num2 == 0);


              compAnswer = num1 / num2;



                  System.out.println(num1+" / " + num2 + "=");                  
                  System.out.print("What's your answer? -->");
                  player = in.nextInt();
                    if (player == compAnswer) {
                        System.out.println(" That's right, the answer is "
                                + compAnswer);
                        System.out.println("");
                        score++;
                    } else {
                        System.out.println("That's wrong! The answer was "
                                + compAnswer);
                        System.out.println("");                     

                    }
            //x++;


      }while( x < numQuestions + 1 );

      System.out.println("");
      System.out.println("Thanks for playing! Your score was " + score +
      "."); }

Solution

  • You are specifically picking numbers where there are a reminder. You can just loop while there is a reminder instead:

    } while (num1 % num2 != 0);
    

    However, you won't need to loop at all. If you pick the second operand and the answer, you can calculate the first operand:

    num2 = (int) (Math.random() *10);
    compAnswer = (int) (Math.random() * 10);
    
    num1 = num2 * compAnswer;