Write a Python program to get the length and the angle of a complex number


This program is written in Python and it uses the cmath module to perform some operations on complex numbers.

  • In the first line of code, the program creates a complex number cn using the complex() function. The real part of cn is 3 and the imaginary part is 4.
  • In the second line of code, the program calculates the length (also called magnitude or modulus) of the complex number cn using the abs() function. The result is printed on the console using the print() function.
  • In the third line of code, the program calculates the angle (also called argument or phase) of the imaginary part of the complex number cn using the cmath.phase() function. Since 0+1j is the imaginary unit i, the angle is equal to the argument of i, which is pi/2 (or 90 degrees). The result is printed on the console using the print() function.
  • The cmath module provides many other functions for working with complex numbers in Python. In this program, we used the abs() and phase() functions, but other functions include cmath.exp(), cmath.log(), cmath.sin(), cmath.cos(), cmath.tan(), cmath.asin(), cmath.acos(), cmath.atan(), and more.

Overall, this program provides a simple implementation of how to perform some operations on complex numbers in Python using the cmath module. It can be used to quickly perform complex number operations in Python.

Source Code

import cmath
cn = complex(3,4)
print("Length of a Complex Number: ", abs(cn))
print("Complex Number Angle : ",cmath.phase(0+1j) )

Output

Length of a Complex Number:  5.0
Complex Number Angle :  1.5707963267948966

Example Programs