{"id":12550,"date":"2021-09-30T17:30:00","date_gmt":"2021-09-30T12:00:00","guid":{"rendered":"https:\/\/python-programs.com\/?p=12550"},"modified":"2021-11-22T18:33:33","modified_gmt":"2021-11-22T13:03:33","slug":"python-program-to-find-magnitude-of-a-complex-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-magnitude-of-a-complex-number\/","title":{"rendered":"Python Program to Find Magnitude of a Complex Number"},"content":{"rendered":"
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners<\/a> tutorial.<\/p>\n Using a simple snippet, we can calculate the Magnitude of a Complex Number in Python. Complex Number:<\/strong> 4+11i<\/p><\/blockquote>\n In the above example, the real part(a) is 4 and the imaginary part(b) is 11. Example:<\/strong><\/p>\n cmplxnumber = complex(9,2)<\/p><\/blockquote>\n In this application, we will utilize the abs()<\/strong> function to determine the magnitude of a complex number. The method abs() only accepts one argument. 1. Modulus of the number. Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the magnitude of a complex number in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial. Using a simple snippet, we can calculate the Magnitude of a Complex Number in Python. Let us begin by learning the fundamentals of complex numbers in Python. First, let’s look at how to initialize …<\/p>\n
\nLet us begin by learning the fundamentals of complex numbers in Python.
\nFirst, let’s look at how to initialize or declare a complex number in Python.<\/p>\n
\nBy setting a variable to a + bi, we can generate a complex number. In this case, a and b are real numbers.<\/p>\n
\nThe complex([real][,imaginary]) method can also be used to generate a complex number. Its parameters are the real and imaginary parts, respectively.
\nIf a parameter is not specified, the complex number’s corresponding portion is set to the default value. The default setting is 0.<\/p>\n
\nThe following is an example of an argument:
\n1. The Float Number
\n2. Complex Number
\n3. Integer
\nIf the number is an integer or a floating-point, abs(number) returns<\/p>\n
\n2. The magnitude of the number if it is complex.<\/p>\nGiven real part = 12\r\nGiven imaginary part = 16<\/pre>\n
The magnitude of the complex number (12+16j) = 20.0<\/pre>\n
Given real part = 11\r\nGiven imaginary part = 47<\/pre>\n
The magnitude of the complex number (11+47j) = 48.27007354458868<\/pre>\n
Python Program to Find Magnitude of a Complex Number<\/h2>\n
\n
Method #1: Using abs Function (Static Input)<\/h3>\n
\n
# Give the real part and imaginary part of the complex number\r\n# as static input and store it in two variables.\r\nrealnumb = 12\r\nimaginarynumb = 16\r\n# Using a complex() function convert those two variables into a complex number.\r\ncomplexnumb = complex(realnumb, imaginarynumb)\r\n# Calculate the magnitude of the complex number using the abs() function.\r\nmagcomplex = abs(complexnumb)\r\n# Print the magnitude of the complex number.\r\nprint('The magnitude of the complex number', complexnumb, '=', magcomplex)\r\n<\/pre>\n
The magnitude of the complex number (12+16j) = 20.0<\/pre>\n
Method #2: Using abs Function (User Input)<\/h3>\n
\n
# Give the real part and imaginary part of the complex number as user input\r\n# using map(), int(), split().\r\n# Store it in two variables.\r\nrealnumb, imaginarynumb = map(int, input(\r\n 'Enter real part and complex part of the complex number = ').split())\r\n# Using a complex() function convert those two variables into a complex number.\r\ncomplexnumb = complex(realnumb, imaginarynumb)\r\n# Calculate the magnitude of the complex number using the abs() function.\r\nmagcomplex = abs(complexnumb)\r\n# Print the magnitude of the complex number.\r\nprint('The magnitude of the complex number', complexnumb, '=', magcomplex)\r\n<\/pre>\n
Enter real part and complex part of the complex number = 11 47\r\nThe magnitude of the complex number (11+47j) = 48.27007354458868<\/pre>\n
\n