Write pseudocode that reads two numbers and divide one by another and display the quotient.
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.
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
Step 1: Use INPUT keyword to accept two numbers (num1 and num2)
Step 2: Use IF/ELSE to check if divisor (num2) is zero
Step 3: If divisor is zero, display error message using PRINT
Step 4: If divisor is not zero, use COMPUTE to calculate quotient = num1 / num2
Step 5: Use PRINT to display the quotient