abs() function for complex number in c++ ?


The abs function in C++ is used to find the absolute value of a complex number. The absolute value of a complex number (also known as modulus) is the distance of that number from the origin in the complex plane. This can be found using the formula −

For complex number a+bi:

mod|a+bi| = √(a2+b2)

The abs() function returns the result of the above calculation in C++. It is defined in the complex library that is needed to be included.

PROGRAM TO SHOW USE OF abs() FUNCTION FOR COMPLEX NUMBERS IN C++

#include <iostream>
#include <complex>
using namespace std;
int main () {
   float a= 13.0 , b = 5.0;
   complex<double> complexnumber (a, b);
   cout << "The absolute value of " << a<<"+"<<b<<"i" << " is: ";
   cout << abs(complexnumber) << endl;
   return 0;
}

Output

The absolute value of 13+5i is: 13.9284

Updated on: 07-Aug-2019

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements