Open In App

norm() function in C++ with Examples

The norm() function is defined in the complex header file. This function is used to return the squared magnitude of the complex number z. Syntax:

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

Parameter:



Return: It returns the squared magnitude of the complex number. 

Time Complexity: O(1)



Auxiliary Space: O(1)

Below programs illustrate the above function:- 

Example 1:- 




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

Output:
The norm of (5,12) is 169

Example 2:- 




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

Output:
The norm of (4,3) is 25

Article Tags :
C++