Modulus of a Complex Number Last Updated : 20 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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: Find the real and imaginary parts, x and y respectively.If z = x +iy Real part = x Imaginary part = yFind the square of x and y separately.Square of Real part = x2 Square of Imaginary part = y2Find the sum of the computed squares.Sum = Square of Real part + Square of Imaginary part = x2 + y2Find 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: 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 number void 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 code int main() { string s = "3+4i"; findModulo(s); return 0; } Java // Java program to find the // Modulus of a Complex Number import java.util.*; class GFG{ // Function to find modulus // of a complex number static 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 code public static void main(String[] args) { String s = "3+4i"; findModulo(s); } } // This code is contributed by Rajput-Ji Python 3 # Python 3 program to find the # Modulus of a Complex Number from math import sqrt # Function to find modulus # of a complex number def 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 code if __name__ == '__main__': s = "3+4i" findModulo(s) # This code is contributed by Surendra_Gangwar C# // C# program to find the // Modulus of a Complex Number using System; public class GFG{ // Function to find modulus // of a complex number static 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 code public static void Main(String[] args) { String s = "3+4i"; findModulo(s); } } // This code contributed by sapnasingh4991 JavaScript // JavaScript program to find the // Modulus of a Complex Number // Function to find modulus // of a complex number function findModulo(s) { let l = s.length; let i, modulus = 0; // Storing the index of '+' if (s.indexOf('+')< l) { i = s.indexOf('+'); } // Storing the index of '-' else { i = s.indexOf('-'); } // Finding the real part // of the complex number let real = s.substring(0, i); // Finding the imaginary part // of the complex number let imaginary = s.substring(i + 1, l - 1); let x = parseInt(real); let y = parseInt(imaginary); console.log(Math.sqrt(x*x + y*y)); } // Driver code let s = "3+4i"; findModulo(s); // The code is contributed by Gautam goel (gautamgoel962) Output:5 Time Complexity: O(1)Auxiliary Space: O(1) As constant extra space is used Comment More infoAdvertise with us Next Article Modulus of a Complex Number spp____ Follow Improve Article Tags : Mathematical Geometric Computer Science Fundamentals DSA Numbers +1 More Practice Tags : GeometricMathematicalNumbers Similar Reads How to compute mod of a big number? Given a big number 'num' represented as string and an integer x, find value of "num % a" or "num mod a". Output is expected as an integer. Examples : Input: num = "12316767678678", a = 10 Output: num (mod a) ? 8 The idea is to process all digits one by one and use the property that xy (mod a) ? ((x 4 min read Modular Exponentiation of Complex Numbers Given four integers A, B, K, M. The task is to find (A + iB)K % M which is a complex number too. A + iB represents a complex number. Examples: Input : A = 2, B = 3, K = 4, M = 5 Output: 1 + i*0 Input : A = 7, B = 3, K = 10, M = 97 Output: 25 + i*29 Prerequisite: Modular Exponentiation Approach: An e 7 min read Multiplication of two complex numbers given as strings Given two complex numbers in the form of strings. Our task is to print the multiplication of these two complex numbers. Examples: Input : str1 = "1+1i" str2 = "1+1i" Output : "0+2i"Here, (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i or "0+2i"Input : str1 = "1+-1i" str2 = "1+-1i"Output : "0+-2i"Here, (1 - 12 min read Square root of two Complex Numbers Given two positive integers A and B representing the complex number Z in the form of Z = A + i * B, the task is to find the square root of the given complex number. Examples: Input: A = 0, B =1Output:The Square roots are: 0.707107 + 0.707107*i-0.707107 - 0.707107*i Input: A = 4, B = 0Output:The Squa 9 min read Find the real and imaginary part of a Complex number Given a complex number Z, the task is to determine the real and imaginary parts of this complex number.Examples: Input: z = 3 + 4i Output: Real part: 3, Imaginary part: 4Input: z = 6 â 8i Output: Real part: 6, Imaginary part: 8 Approach: A complex number can be represented as Z = x + yi, where x is 5 min read Geometry using Complex Numbers in C++ | Set 2 After going through previous post, we know what exactly are complex numbers and how we can use them to simulate points in a cartesian plane. Now, we will have an insight as to how to use the complex class from STL in C++.To use the complex class from STL we use #include <complex> Defining Poin 5 min read Geometry using Complex Numbers <std::complex> in C++ | Set 1 While solving geometric problems, it is time consuming to define the point class for specifying a point on the 2D Plane or the Euclidean Plane. So, this article specifies a faster and clever way to implement the same using complex class from STL in C++. Before implementation, it is essential to unde 5 min read Modular Arithmetic for Competitive Programming In mathematics, modular arithmetic refers to the arithmetic of integers that wraps around when a certain value is reached, called the modulus. This becomes particularly crucial when handling large numbers in competitive programming. This article "Modular Arithmetic for Competitive Programming" will 11 min read Convert given Complex Numbers into polar form and perform all arithmetic operations Given two Complex Numbers Z1 and Z2 in the Cartesian form, the task is to convert the given complex number into polar form and perform all the arithmetic operations ( addition, subtraction, multiplication, and division ) on them. Examples: Input: Z1 = (2, 3), Z2 = (4, 6)Output:Polar form of the firs 15 min read Program for quotient and remainder of big number Given a string of numbers and given another number (say m) [0 <= m <= 10^18]. The task is to calculate the modulus of the given number.Examples: Input : num = "214" m = 5 Output : Remainder = 4 Quotient = 42 Input : num = "214755974562154868" m = 17 Output : Remainder = 15 Quotient = 126327043 8 min read Like