Question 2 of 16intermediate🔧 ApplyLong Answer5 marks

Write pseudocode that reads two numbers and divide one by another and display the quotient.

Correct Answer

The pseudocode to read two numbers, divide one by another, and display the quotient is given below:

INPUT num1 INPUT num2 IF num2 = 0 THEN PRINT "Cannot divide by zero" ELSE COMPUTE quotient = num1 / num2 PRINT quotient ENDIF

In this pseudocode, the INPUT keyword is used to accept two numbers from the user. The first number (num1) acts as the dividend and the second number (num2) acts as the divisor.

The pseudocode uses IF/ELSE for decision making to handle a special case. It checks whether the divisor (num2) is zero, because division by zero is undefined and would cause an error. If num2 equals zero, an appropriate error message is displayed.

If the divisor is not zero, the COMPUTE keyword is used to calculate the quotient by dividing num1 by num2. The result is stored in a variable called quotient.

Finally, the PRINT keyword displays the computed quotient to the user. This algorithm demonstrates how sequence and selection structures work together to solve a problem while handling potential errors.

Exercise: EXERCISE | Q: 1 | (Page 23)
For More Understanding

Explanation

The textbook context provides examples of pseudocode using keywords like INPUT, COMPUTE, PRINT, and IF/ELSE. Following the pattern from Example 4.3 (sum of two numbers), this pseudocode adapts it for division. The IF/ELSE structure handles division by zero, which is a necessary consideration since the context mentions 'decision making involves selection of one of the alternatives based on outcome of a condition.'

Solution Steps

  1. Step 1: Use INPUT keyword to accept two numbers (num1 and num2)

  2. Step 2: Use IF/ELSE to check if divisor (num2) is zero

  3. Step 3: If divisor is zero, display error message using PRINT

  4. Step 4: If divisor is not zero, use COMPUTE to calculate quotient = num1 / num2

  5. Step 5: Use PRINT to display the quotient