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, operator):
if operator == '+':
return add(num1, num2)
elif operator == '-':
return subtract(num1, num2)
elif operator == '*':
return multiply(num1, num2)
elif operator == '/':
return divide(num1, num2)
else:
return "Error: Invalid operator"
# 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 first define four functions add, subtract, multiply, and divide for the four arithmetic operations. Then we define a function calculate that takes two numbers and an operator as input, and calls the appropriate function based on the operator. If the operator is not one of the four valid operators, it returns an error message. Finally, we test the calculate function with some example inputs.
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.
Create a C++ programme that accomplishes all arithmetic operations on two complex objects, including addition, subtraction, multiplication, and division. (X + i * Y) represents the Complex Object (Where X and Y are two real numbers and 'i' is an Imaginary). As a programmer, create a class called "COMPLEX" containing the data members X and Y and also getData() and putData() methods to pefrom read and write for the above said Data Members. Define a separate functions for the operations such as Addition, Subtraction, Multiplication, and Division.Input :Read the Two Complex Objects (Each with X and Y)Output :Addition of Two Complex ObjectsSubtraction of Two Complex ObjectsMultiplication of Two Complex ObjectsDivision of Two Complex ObjectsNote :Both X and Y are Non-Negative, otherwise print "Invalid"Define a separate functions with return type and with arguments of two complext objects.
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.