real() function in C++

The real() function is defined in the complex header file. The real() function is used to find the real part of the complex number.

Syntax:

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

Parameter: This method takes a mandatory parameter z: which represents the given complex number.

Return: It returns the real part of the specified complex number.

Below programs illustrate the above function:-

Example 1:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// example of real() function.
  
#include <bits/stdC++.h>
using namespace std;
  
// driver function
int main()
{
    // defines the complex number: (20.3 + 4.9i)
    complex<double> realpart(20.3, 4.9);
  
    // print the complex number
    cout << "Complex Number = "
         << realpart << endl;
  
    // prints the real part using the real function
    cout << "Real part of the complex number is ="
         << real(realpart) << endl;
  
    return 0;
}

chevron_right


Output:

Complex Number = (20.3, 4.9)
Real part of the complex number is =20.3

Example 2:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// example of real() function.
  
#include <bits/stdC++.h>
using namespace std;
  
// driver function
int main()
{
    // defines the complex number: (2 + 2i)
    complex<double> realpart(2, 2);
  
    // print the complex number
    cout << "Complex Number = "
         << realpart << endl;
  
    // prints the real part using the real function
    cout << "Real part of the complex number is ="
         << real(realpart) << endl;
  
    return 0;
}

chevron_right


Output:

Complex Number = (2, 2)
Real part of the complex number is =2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.