real( ) function in c++

C++Server Side ProgrammingProgramming

Given is the task to the working of real( ) function in C++.

This function is used to find the real part of the complex number. This function return the real part of the complex number and sets the real part to value. And real returns the real component. The real( ) function is the function of <complex> header file.

In complex number a+Bi , the Bi is a imaginary part and A is the Real part of the complex number.

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

Syntax

Template<class Y> Y
Real(constant complex<Y>& X);

Parameters − The parameter X which represents the given complex number.

Example

Output – Complex Number = (14.7, 6.7)

Real Part of complex number = 14.7

Output – Complex Number = (12, 12)

Real Part of complex number = 12

Approach can be followed

  • First we define the complex number.

  • Then we print the Complex Number.

  • Then we print the Real part of Complex Number.

By using above approach we can get the desired output. The real function will print the real part of the complex number. The function returns the same as if calling: X.real( ).

Algorithm

Start-
STEP 1 – Create the main ( ) function and defines the complex number.
   complex<double> realpart(3.6, 2.4)
END
STEP 2 - Then print the real part using the real function.
   cout<< “ Real part of Complex number “<< real(realpart) << endl;
END
Stop

Example

// C++ code to demonstrate the working of real( ) function
#include<iostream.h>
#include<complex.h>
Using namespace std;
int main( ){
   //defines the complex number
   Complex<defines> real_part(17.6, 4.5) ;
   cout << “ Complex Number = “<<real_part << endl;
   // prints the real part using the real function
   cout<< “ Real part of complex number =”<< real(real_part) <<endl;
   return 0;
}

Output

If we run the above code then it will generate the following output

Output – Complex Number = (17.6, 4.5)
   Real part of complex number = 17.6
Output – Complex Number = (14, 17)
   Real part of complex number = 14
raja
Updated on 28-Feb-2020 06:19:23

Advertisements