In: Computer Science
This program must have 9 functions:
•main() Controls the flow of the program (calls the other modules)
•userInput() Asks the user to enter two numbers
•add() Accepts two numbers, returns the sum
•subtract() Accepts two numbers, returns the difference of the first number minus the second number
•multiply() Accepts two numbers, returns the product
•divide() Accepts two numbers, returns the quotient of the first number divided by the second number
•modulo() Accepts two numbers, returns the modulo of the first number divided by the second number
•exponent() Accepts two numbers, returns the first number raised to the power of the second number
•userOutput() Gives a user friendly output of all the calculations.
NOTE: The ONLY place that you will print results is inside the userOutput function
#method to get input
def userInput():
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
return a, b
#method to add two numbers
def add(a, b):
return a+b
#method to subtract two numbers
def subtract(a, b):
return a-b
#method to multiply two numbers
def multiply(a, b):
return a*b
#method to divide two numbers
def divide(a, b):
return a/b
#method to modulo two numbers
def modulo(a, b):
return a%b
#method to exponent two numbers
def exponent(a, b):
return pow(a, b)
#method to display the result
def userOutput(a, b, result, op):
print(a,op,b,"=",result)
def main():
#method calling and display result
a, b = userInput()
s = add(a, b)
userOutput(a, b, s, '+')
#method calling and display result
s = subtract(a, b)
userOutput(a, b, s, '-')
#method calling and display result
s = multiply(a, b)
userOutput(a, b, s, '*')
#method calling and display result
s = divide(a, b)
userOutput(a, b, s, '/')
#method calling and display result
s = modulo(a, b)
userOutput(a, b, s, '%')
#method calling and display result
s = exponent(a, b)
userOutput(a, b, s, '^')
#main method calling
main()
The screenshot is given below:
OUTPUT:
Enter the first number: 3
Enter the second number: 5
3 + 5 = 8
3 - 5 = -2
3 * 5 = 15
3 / 5 = 0.6
3 % 5 = 3
3 ^ 5 = 243