Sheerpower Logo
Q.2  A Simple Calculator

A Simple Calculator

This is a simple interactive calculator. The code follows, and following that is a detailed explanation. Feel free to copy this code and modify it.
// Simple Calculator in Sheerpower do // Get user input for the first number input "Enter the first number": num1 // Get user input for the operation input "Enter the operation (+, -, *, /)": operation$ // Get user input for the second number input "Enter the second number": num2 // Perform the calculation based on the operation if operation$ = "+" then result = num1 + num2 elseif operation$ = "-" then result = num1 - num2 elseif operation$ = "*" then result = num1 * num2 elseif operation$ = "/" then // Check for division by zero if num2 = 0 then print "?? Error: Division by zero is not allowed!" iterate do else result = num1 / num2 end if else print "?? Invalid operation! Please enter one of +, -, *, /." iterate do end if // Display the result print "The result is: "; result // Ask the user if they want to perform another calculation input "Do you want to perform another calculation? (yes/no)": yn$ if ucase$(yn$) <> "Y" then exit do end if loop print "Thank you for using the calculator!"

Explanation:

1. Input Handling: The program prompts the user to enter two numbers and an arithmetic operation.

2. Operation Execution: It checks which operation the user selected and performs the corresponding calculation.

3. Division by Zero: The program includes a check to ensure the user does not divide by zero, which would cause an error.

4. Looping: After displaying the result, the program asks the user if they want to perform another calculation. If the user says "yes," the program continues; otherwise, it exits.

5. End of Program: The program ends with a thank you message.

Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen