VOOZH about

URL: https://www.geeksforgeeks.org/dsa/modulus-of-a-complex-number/

⇱ Modulus of a Complex Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Modulus of a Complex Number

Last Updated : 12 Jul, 2025

Given a complex number z, the task is to determine the modulus of this complex number. Note: Given a complex number z = a + ib the modulus is denoted by |z| and is defined as [latex]\left | z \right | = \sqrt{a^{2}+b^{2}}[/latex] Examples:

Input: z = 3 + 4i 
Output: 5 |z| = (32 + 42)1/2 = (9 + 16)1/2 = 5 

Input: z = 6 - 8i 
Output: 10 
Explanation: |z| = (62 + (-8)2)1/2 = (36 + 64)1/2 = 10

Approach: For the given complex number z = x + iy:

  1. Find the real and imaginary parts, x and y respectively.
If z = x +iy

Real part = x
Imaginary part = y
  1. Find the square of x and y separately.
Square of Real part = x2
Square of Imaginary part = y2
  1. Find the sum of the computed squares.
Sum = Square of Real part 
 + Square of Imaginary part
 = x2 + y2
  1. Find the square root of the computed sum. This will be the modulus of the given complex number
[latex]\left | z \right | = \sqrt{x^{2}+y^{2}}[/latex]

Below is the implementation of the above approach: 

Output:
5

Time Complexity: O(1)
Auxiliary Space: O(1)

As constant extra space is used

Comment
Article Tags:
Article Tags: