Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively.
Question
Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively.
Solution
Sure, here is a simple program in Python that performs basic arithmetic operations:
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def multiply(num1, num2):
return num1 * num2
def divide(num1, num2):
if num2 != 0:
return num1 / num2
else:
return "Error: Division by zero is not allowed"
def calculate(num1, num2, operation):
if operation == '+':
return add(num1, num2)
elif operation == '-':
return subtract(num1, num2)
elif operation == '*':
return multiply(num1, num2)
elif operation == '/':
return divide(num1, num2)
else:
return "Error: Invalid operation"
# Test the function
print(calculate(10.5, 2.5, '+'))
print(calculate(10.5, 2.5, '-'))
print(calculate(10.5, 2.5, '*'))
print(calculate(10.5, 2.5, '/'))
In this program, we have defined a function for each arithmetic operation. The calculate function takes two numbers and an operation as input, and calls the appropriate function based on the operation. If the operation is not one of the four specified, it returns an error message.
Similar Questions
Write a program to enter two numbers and print the arithmeticoperations like +,-,*,**, /, // and % using function
Charlie, a novice programmer, is developing a simple calculator program to perform basic arithmetic operations. He needs your assistance in refining the program.Write a program that takes an operator ('+', '-', '*', or '/') and two operands as input. The program should then perform the corresponding operation and display the result. If the input is an invalid operator, the program should display an appropriate error message.Function Specifications:double add(double x, double y)double subtract(double x, double y)double multiply(double x, double y)double divide(double x, double y)Input format :The first line of input consists of a character c, representing the operator (+, -, *, /).The second line consists of two space-separated double values a and b, representing the operands.Output format :
Create a mini calculator which has the ability to add, subtract, multiply and divide. Follow the steps below to complete this assignment.Step 1: Your code should be able to add, subtract, divide and multiplyStep 2: Your program should ask the user for two numbers.Step 3: Your code should calculate and print the output for all the calculations. Use appropriate variables and output names.
.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.
Program that performs arithmetic operations with values of type char.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.