Open In App

abs() function for complex number in c++

The abs() function for complex number is defined in the complex header file. This function is used to return the absolute value of the complex number z.

Syntax:



template<class T> T abs (const complex<T>& z);

Parameter:

Return: It returns the absolute value of the complex number.



Below programs illustrate the above function:-

Example 1:-




// C++ program to demonstrate
// example of abs() function.
   
#include <bits/stdc++.h>
using namespace std;
   
// driver function
int main ()
{
  // defines the complex number: (5.0+12.0i)
  complex<double> complexnumber (5.0, 12.0);
   
  // prints the absolute value of the complex number
  cout << "The absolute value of " << complexnumber << " is: ";
  cout << abs(complexnumber) << endl;
   
 return 0;
}

Output:
The absolute value of (5,12) is: 13

Example 2:-




// C++ program to demonstrate
// example of abs() function.
  
#include <bits/stdc++.h>
using namespace std;
  
// driver function
int main ()
{
  // defines the complex number: (4.0+3.0i)
  complex<double> complexnumber (4.0, 3.0);
  
  // prints the absolute value of the complex number
  cout << "The absolute value of " << complexnumber << " is: ";
  cout << abs(complexnumber) << endl;
  
  
  return 0;
}

Output:
The absolute value of (4,3) is: 5

Article Tags :