We recommend using Visual Studio 2017

Step 7: Add Multiplication and Division Problems


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Step 7: Add Multiplication and Division Problems.

In the seventh part of this tutorial, you'll add multiplication and division problems, but first think about how to make that change. Consider the initial step, which involves storing values.

To add multiplication and division problems

  1. Add four more integer variables to the form.

        public partial class Form1 : Form
            // Create a Random object called randomizer 
            // to generate random numbers.
            Random randomizer = new Random();
            // These integer variables store the numbers 
            // for the addition problem. 
            int addend1;
            int addend2;
            // These integer variables store the numbers 
            // for the subtraction problem. 
            int minuend;
            int subtrahend;
            // These integer variables store the numbers 
            // for the multiplication problem. 
            int multiplicand;
            int multiplier;
            // These integer variables store the numbers 
            // for the division problem. 
            int dividend;
            int divisor;
            // This integer variable keeps track of the 
            // remaining time.
            int timeLeft;

  2. As you did before, modify the StartTheQuiz() method to fill in random numbers for the multiplication and division problems.

            /// <summary>
            /// Start the quiz by filling in all of the problem 
            /// values and starting the timer. 
            /// </summary>
            public void StartTheQuiz()
                // Fill in the addition problem.
                // Generate two random numbers to add.
                // Store the values in the variables 'addend1' and 'addend2'.
                addend1 = randomizer.Next(51);
                addend2 = randomizer.Next(51);
                // Convert the two randomly generated numbers
                // into strings so that they can be displayed
                // in the label controls.
                plusLeftLabel.Text = addend1.ToString();
                plusRightLabel.Text = addend2.ToString();
                // 'sum' is the name of the NumericUpDown control.
                // This step makes sure its value is zero before
                // adding any values to it.
                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 it also checks the multiplication and division problems.

            /// <summary>
            /// Check the answers 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;

    You can't easily enter the multiplication sign (×) and the division sign (÷) using the keyboard, so Visual C# and Visual Basic accept an asterisk (*) for multiplication and a slash mark (/) for division.

  4. Change the last part of the timer's Tick event handler so that it fills in the correct answer when time runs out.

                    // 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.

    Quiz takers must answer four problems to complete the quiz, as the following illustration shows.

    Math quiz with four problems
    Math quiz with four problems

To continue or review