A.1. Chapter 3

A.1.1. Exercise 1 Solution

The code for this solution is exactly the same as for the integer division solution provided earlier in the chapter, with the exception that the line that reads

answer = operand1 / operand2;

in the code should be changed to:

answer = operand1 * operand2;

The remainder of the program is the same. It is important to try very large and very small numbers to see what happens when your answer falls outside the range of the data type you selected.

A.1.2. Exercise 2 Solution

You can use most of the code shown in the chapter; the only difference is that you do not need to have the user enter two input values. In this program, you want to prompt them only for the Fahrenheit temperature. Therefore, the relevant code becomes

private void btnCalc_Click(object sender, EventArgs e)
{
  bool flag;
  double operand1;
  double answer;

       //                    Input Step
       // Check first input. . .
  flag = double.TryParse(txtOperand1.Text, out operand1);
if (flag == false)
  {
    MessageBox.Show("Enter Fahrenheit temperature", "Input Error");
    txtOperand1.Focus();
    return;
  }


       //                     Process Step

  answer = 5.0 / 9.0 * (operand1 - 32);

       //                     Display Step
  txtResult.Text = operand1.ToString() + " is " +
                    answer.ToString() + " Celsius";
  txtResult.Visible = true;

}

You can use the same textbox to display the answer. If you wrote the equation as:

answer = .555555555555555 * (operand1 - 32);

treat yourself to an extra scoop of ice cream. Division is one of the slowest math operations a computer performs, and ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.