Complex Number Magnitude

Medium ⏱ 12 min 46% acceptance ★★★★☆ 4.3
Write a function magnitude(z) that takes a Python complex number z and returns its magnitude (absolute value), rounded to 2 decimal places. Recall a complex number has a .real and .imag part.

Examples

Example 1
Input
magnitude(3 + 4j)
Output
5.0
Explanation

sqrt(3^2 + 4^2) = 5.

Example 2
Input
magnitude(1 + 1j)
Output
1.41
Explanation

sqrt(2) rounded to 2 decimals.

Constraints

  • `z` is always a valid Python complex number.

Topics

Variables & Data Typescomplex numbers

Companies

IntelOracle

Hints

Hint 1

The built-in `abs()` works directly on complex numbers.

Hint 2

Round the result with `round(value, 2)`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.