94.1k views
3 votes
Write a function max_magnitude() with two integer input parameters that returns the largest magnitude value. Use the function in a program that takes two integer inputs, and outputs the largest magnitude value. Ex: If the inputs are: 5 7 the function returns: 7 Ex: If the inputs are: -8 -2 the function returns: -8 Note: The function does not just return the largest value, which for -8 -2 would be -2. Though not necessary, you may use the built-in absolute value function to determine the max magnitude, but you must still output the input number (Ex: Output -8, not 8). Your program must define and call the following function:

1 Answer

4 votes

Answer:

The question is answered in python

def max_magnitude(a,b):

if abs(a) > abs(b):

return a

else:

return b

num1 = int(input("Num 1: "))

num2 = int(input("Num 2: "))

print(max_magnitude(num1,num2))

Step-by-step explanation:

This question is answered using the concept of absolute value to get the integer with the highest magnitude

First, the function is defined with integer parameters a and b

def max_magnitude(a,b):

This checks if a has the highest magnitude

if abs(a) > abs(b):

If true, it returns a

return a

If otherwise, it returns b

else:

return b

The main starts here

The next two lines prompt the user for inputs

num1 = int(input("Num 1: "))

num2 = int(input("Num 2: "))

This calls the function

print(max_magnitude(num1,num2))

User Prassee
by
4.5k points