Question

In: Computer Science

This program must have 9 functions: •main() Controls the flow of the program (calls the other...

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


python program

Solutions

Expert Solution

#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



Related Solutions

python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: 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,...
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
1G. This program, unlike the previous 6 programs, will have several functions in it: besides main,...
1G. This program, unlike the previous 6 programs, will have several functions in it: besides main, it willhave the following 7 functions:public static int triangle( int n )public static int multiply( int a, int b )public static void square( int size )public static void hollowSquare( int size )public static int factorial( int n )public static long fibonacci( int n )public static boolean prime( long n )These 7 functions should work as described below.Your main program should call each of these...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
This program must be done in JAVA. Write a program to monitor the flow of an...
This program must be done in JAVA. Write a program to monitor the flow of an item into an out of a warehouse. The warehouse has numerous deliveries and shipments for this item (a widget) during the time period covered. A shipment out (an order) is billed at a profit of 50% over the cost of the widget. Unfortunately each incoming shipment may have a different cost associated with it. The accountants of the firm have instituted a last-in, first...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
Split the main function given into multiple functions. You have been given a very simple program...
Split the main function given into multiple functions. You have been given a very simple program that performs basic operations (addition, subtraction, editing) on two randomly generated integer vectors. All functionality has been included in main, causing code segments to be repeated as well as diminishing the readability. Rewrite the program by grouping calculations and related operations into functions. In particular, your program should include the following functions. InitializeVectors: This is a void function that initializes the two vectors by...
[Procedures] Write a main program which sets the registers BX and CX and calls a procedure...
[Procedures] Write a main program which sets the registers BX and CX and calls a procedure Add-Two. The procedure Add-Two adds the values in registers BX and CX and returns the output (which is the sum) in AX. Single step through the program, displaying the value of the stack pointer so that you understand how the call and return are implemented.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT