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...
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...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT