Prompt
Answer
Pseudo Code for Simple Calculator Program
Purpose
To create a program that prompts the user for two integer values and an arithmetic operation (addition, subtraction, multiplication, or division) to perform on those values.
Steps
Start Program
- Initialize the program.
Input User Values
- Prompt the user to enter the first integer.
- Store the first integer as
firstValue
. - Prompt the user to enter the second integer.
- Store the second integer as
secondValue
.
Choose Operation
- Display a menu of operations to the user:
- "1: Addition"
- "2: Subtraction"
- "3: Multiplication"
- "4: Division"
- Prompt the user to select an operation.
- Store the selected operation as
operationChoice
.
- Display a menu of operations to the user:
Perform Calculation
- Initialize a variable
result
to hold the output. - Use conditional statements to determine the selected operation:
- If
operationChoice
is 1:- Set
result = firstValue + secondValue
- Set
- Else if
operationChoice
is 2:- Set
result = firstValue - secondValue
- Set
- Else if
operationChoice
is 3:- Set
result = firstValue * secondValue
- Set
- Else if
operationChoice
is 4:- If
secondValue
is not 0:- Set
result = firstValue / secondValue
- Set
- Else:
- Display an error message for division by zero.
- If
- Else:
- Display an error message for invalid operation choice.
- If
- Initialize a variable
Display Result
- Print
result
.
- Print
End Program
- Terminate the program execution.
Pseudo Code
BEGIN Program
// Input User Values
PRINT "Enter first integer:"
INPUT firstValue
PRINT "Enter second integer:"
INPUT secondValue
// Choose Operation
PRINT "Choose an operation:"
PRINT "1: Addition"
PRINT "2: Subtraction"
PRINT "3: Multiplication"
PRINT "4: Division"
INPUT operationChoice
// Perform Calculation
IF operationChoice == 1 THEN
result = firstValue + secondValue
ELSE IF operationChoice == 2 THEN
result = firstValue - secondValue
ELSE IF operationChoice == 3 THEN
result = firstValue * secondValue
ELSE IF operationChoice == 4 THEN
IF secondValue != 0 THEN
result = firstValue / secondValue
ELSE
PRINT "Error: Division by zero."
END IF
ELSE
PRINT "Error: Invalid operation choice."
END IF
// Display Result
PRINT "Result: " + result
END Program
Summary
The pseudo code outlines a simple calculator program. It captures user inputs, processes an arithmetic operation based on user choice, and handles basic errors such as division by zero and invalid choices. This structured approach ensures clarity and facilitates understanding of the logic underlying the program.
Description
This pseudo code outlines a user-friendly calculator program that prompts for two integers and an arithmetic operation, performs calculations, and handles errors like division by zero while displaying the result.