This documentation is archived and is not being maintained.

Step 7: Add Multiplication and Division Problems

Before you begin this procedure, think about how you will add multiplication and division problems. Consider the initial step, which involves storing values.

To add multiplication and division problems

  1. Add four ints (Integers) to the form. The code should look like the following.

    public partial class Form1 : Form
        // Create a Random object to generate random numbers.
        Random randomizer = new Random();
        // These ints will store the numbers for the addition problem.
        int addend1;
        int addend2;
        // These ints will store the numbers for the subtraction problem.
        int minuend;
        int subtrahend;
        // These ints will store the numbers for the multiplication problem.
        int multiplicand;
        int multiplier;
        // These ints will store the numbers for the division problem.
        int dividend;
        int divisor;
        // This int will keep track of the time left.
        int timeLeft;
  2. Like before, modify the StartTheQuiz() method to fill in random multiplication and division problems. The code should look like the following.

    /// <summary>
    /// Start the quiz by filling in all of the problems
    /// and starting the timer.
    /// </summary>
    public void StartTheQuiz()
        // Fill in the addition problem.
        addend1 = randomizer.Next(51);
        addend2 = randomizer.Next(51);
        plusLeftLabel.Text = addend1.ToString();
        plusRightLabel.Text = addend2.ToString();
        sum.Value = 0;
        // Fill in the subtraction problem.
        minuend = randomizer.Next(1, 101);
        subtrahend = randomizer.Next(1, minuend);
        minusLeftLabel.Text = minuend.ToString();
        minusRightLabel.Text = subtrahend.ToString();
        difference.Value = 0;
        // Fill in the multiplication problem.
        multiplicand = randomizer.Next(2, 11);
        multiplier = randomizer.Next(2, 11);
        timesLeftLabel.Text = multiplicand.ToString();
        timesRightLabel.Text = multiplier.ToString();
        product.Value = 0;
        // Fill in the division problem.
        divisor = randomizer.Next(2, 11);
        int temporaryQuotient = randomizer.Next(2, 11);
        dividend = divisor * temporaryQuotient;
        dividedLeftLabel.Text = dividend.ToString();
        dividedRightLabel.Text = divisor.ToString();
        quotient.Value = 0;
        // Start the timer.
        timeLeft = 30;
        timeLabel.Text = "30 seconds"; 
  3. Modify the CheckTheAnswer() method so that the method also checks the multiplication and division problems. The code should look like the following.

    /// <summary>
    /// Check the answer to see if the user got everything right.
    /// </summary>
    /// <returns>True if the answer's correct, false otherwise.</returns>
    private bool CheckTheAnswer()
        if ((addend1 + addend2 == sum.Value)
            && (minuend - subtrahend == difference.Value)
            && (multiplicand * multiplier == product.Value)
            && (dividend / divisor == quotient.Value))
            return true;
            return false;

    Because there's no simple way to enter the multiplication sign (×) and the division sign (÷) using the keyboard, Visual C# and Visual Basic use an asterisk (*) for multiplication and a slash mark (/) for division.

  4. Change the last part of the timer's Tick event handler so it fills in the correct answer when time runs out. The code should look like the following.

        // If the user ran out of time, stop the timer, show
        // a MessageBox, and fill in the answers.
        timeLabel.Text = "Time's up!";
        MessageBox.Show("You didn't finish in time.", "Sorry");
        sum.Value = addend1 + addend2;
        difference.Value = minuend - subtrahend;
        product.Value = multiplicand * multiplier;
        quotient.Value = dividend / divisor;
        startButton.Enabled = true;
  5. Save and run your program. Now, four problems must be answered to complete the quiz, as shown in the following picture.

    Math quiz with four problems

    Math quiz with four problems

To continue or review