Modulus of a Complex Number - GeeksforGeeks

15 Apr, 2020
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 
Examples:
Input: z = 3 + 4iOutput: 5|z| = (32 + 42)1/2 = (9 + 16)1/2 = 5
Input: z = 6 – 8iOutput: 10Explanation:|z| = (62 + (-8)2)1/2 = (36 + 64)1/2 = 10
Approach: For the given complex number z = x + iy:
Find the real and imaginary parts, x and y respectively.If z = x +iy

Real part = x
Imaginary part = y
Find the square of x and y separately.Square of Real part = x2
Square of Imaginary part = y2
Find the sum of the computed squares.Sum = Square of Real part 
      + Square of Imaginary part
    = x2 + y2
Find the square root of the computed sum. This will be the modulus of the given complex number
Find the real and imaginary parts, x and y respectively.If z = x +iy

Real part = x
Imaginary part = y

If z = x +iy

Real part = x
Imaginary part = y

Find the square of x and y separately.Square of Real part = x2
Square of Imaginary part = y2

Square of Real part = x2
Square of Imaginary part = y2

Find the sum of the computed squares.Sum = Square of Real part 
      + Square of Imaginary part
    = x2 + y2

Sum = Square of Real part 
      + Square of Imaginary part
    = x2 + y2

Find the square root of the computed sum. This will be the modulus of the given complex number
Below is the implementation of the above approach:
C++
Java
Python 3
C#
// C++ program to find the// Modulus of a Complex Number  #include <bits/stdc++.h>using namespace std;  // Function to find modulus// of a complex numbervoid findModulo(string s){    int l = s.length();    int i, modulus = 0;      // Storing the index of '+'    if (s.find('+') < l) {        i = s.find('+');    }    // Storing the index of '-'    else {        i = s.find('-');    }      // Finding the real part    // of the complex number    string real = s.substr(0, i);      // Finding the imaginary part    // of the complex number    string imaginary = s.substr(i + 1, l - 1);      int x = stoi(real);    int y = stoi(imaginary);      cout << sqrt(x * x + y * y) << "\n";}  // Driver codeint main(){    string s = "3+4i";      findModulo(s);      return 0;}
// Java program to find the// Modulus of a Complex Numberimport java.util.*;  class GFG{   // Function to find modulus// of a complex numberstatic void findModulo(String s){    int l = s.length();    int i, modulus = 0;       // Storing the index of '+'    if (s.contains("+")) {        i = s.indexOf("+");    }      // Storing the index of '-'    else {        i = s.indexOf("-");    }       // Finding the real part    // of the complex number    String real = s.substring(0, i);       // Finding the imaginary part    // of the complex number    String imaginary = s.substring(i + 1, l-1);       int x = Integer.parseInt(real);    int y = Integer.parseInt(imaginary);       System.out.print(Math.sqrt(x * x + y * y)+ "\n");}   // Driver codepublic static void main(String[] args){    String s = "3+4i";       findModulo(s);}}  // This code is contributed by Rajput-Ji
# Python 3 program to find the# Modulus of a Complex Numberfrom math import sqrt  # Function to find modulus# of a complex numberdef findModulo(s):    l = len(s)    modulus = 0      # Storing the index of '+'    if ( '+' in s ):        i = s.index('+')      # Storing the index of '-'    else:        i = s.index('-')      # Finding the real part    # of the complex number    real = s[0:i]      # Finding the imaginary part    # of the complex number    imaginary = s[i + 1:l - 1]      x = int(real)    y = int(imaginary)      print(int(sqrt(x * x + y * y)))  # Driver codeif __name__ == '__main__':    s = "3+4i"      findModulo(s)  # This code is contributed by Surendra_Gangwar
// C# program to find the// Modulus of a Complex Numberusing System;  public class GFG{    // Function to find modulus// of a complex numberstatic void findModulo(String s){    int l = s.Length;    int i;        // Storing the index of '+'    if (s.Contains("+")) {        i = s.IndexOf("+");    }       // Storing the index of '-'    else {        i = s.IndexOf("-");    }        // Finding the real part    // of the complex number    String real = s.Substring(0, i);        // Finding the imaginary part    // of the complex number    String imaginary = s.Substring(i + 1, l-i - 2);        int x = Int32.Parse(real);    int y = Int32.Parse(imaginary);        Console.Write(Math.Sqrt(x * x + y * y)+ "\n");}    // Driver codepublic static void Main(String[] args){    String s = "3+4i";        findModulo(s);}}// This code contributed by sapnasingh4991
5

SURENDRA_GANGWAR
Rajput-Ji
sapnasingh4991
Numbers
Geometric
Mathematical
School Programming
Mathematical
Numbers
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Haversine formula to find distance between two points on a sphere
Equation of circle when three points on the circle are given
Program to find slope of a line
Program to find line passing through 2 Points
Maximum Manhattan distance between a distinct pair from N coordinates
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7