Table of Contents
Algorithm
the algorithm to take two operands and one operator from the user, perform the operation using a switch
statement, and print the result:
- Prompt the user to enter the first operand and store it in a variable, let’s call it
operand1
. - Prompt the user to enter the second operand and store it in a variable, let’s call it
operand2
. - Prompt the user to enter the operator (+, -, *, /, %) and store it in a variable, let’s call it
operator
. - Initialize a variable, let’s call it
result
, to hold the calculated result. - Use a
switch
statement with theoperator
variable as the expression. - For each case in the
switch
statement, perform the corresponding operation based on the entered operator. - Store the result of the operation in the
result
variable. - Print the value of
result
as the calculated result.
Enter the first operand: 5 Enter the second operand: 3 Enter the operator (+, -, *, /, %): * Step 1: operand1 = 5 Step 2: operand2 = 3 Step 3: operator = '*' Step 4: Initialize result Step 5: Evaluate the 'switch' statement with the 'operator' variable - Case '*': - Perform the multiplication: result = 5 * 3 = 15 - Exit the 'switch' statement Step 9: Print the value of result: 15 Output: The result is 15
C Program
#include <stdio.h> int main() { float operand1, operand2; char operator; float result; printf("Enter the first operand: "); scanf("%f", &operand1); printf("Enter the second operand: "); scanf("%f", &operand2); printf("Enter the operator (+, -, *, /, %%): "); scanf(" %c", &operator); switch (operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; case '%': result = (int)operand1 % (int)operand2; break; default: printf("Invalid operator\n"); return 0; } printf("The result is: %.2f\n", result); return 0; }
C++
#include <iostream> using namespace std; int main() { float operand1, operand2; char operator; float result; cout << "Enter the first operand: "; cin >> operand1; cout << "Enter the second operand: "; cin >> operand2; cout << "Enter the operator (+, -, *, /, %): "; cin >> operator; switch (operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; case '%': result = (int)operand1 % (int)operand2; break; default: cout << "Invalid operator" << endl; return 0; } cout << "The result is: " << result << endl; return 0; }
In both versions, the program takes two operands (operand1
and operand2
) and an operator (operator
) as inputs from the user. It then uses a switch
statement to perform the desired operation based on the provided operator. The result is stored in the result
variable and printed to the console.
Python
def calculate_result(operand1, operand2, operator): result = 0 # Perform the operation based on the operator switch_operator = { '+': operand1 + operand2, '-': operand1 - operand2, '*': operand1 * operand2, '/': operand1 / operand2, '%': operand1 % operand2 } result = switch_operator.get(operator, "Invalid operator") return result # Take input from the user operand1 = float(input("Enter the first operand: ")) operand2 = float(input("Enter the second operand: ")) operator = input("Enter the operator (+, -, *, /, %): ") # Calculate and print the result result = calculate_result(operand1, operand2, operator) print("The result is:", result)
this program, the calculate_result
function takes three inputs: operand1
, operand2
, and operator
. It uses a dictionary called switch_operator
to map each operator to its respective operation. The get
method of the dictionary is used to fetch the value based on the provided operator. If the operator is not found in the dictionary, the program returns “Invalid operator”.
Java
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first operand: "); double operand1 = input.nextDouble(); System.out.print("Enter the second operand: "); double operand2 = input.nextDouble(); System.out.print("Enter the operator (+, -, *, /, %): "); char operator = input.next().charAt(0); double result; switch (operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; case '%': result = operand1 % operand2; break; default: System.out.println("Invalid operator"); return; } System.out.println("The result is: " + result); input.close(); } }