Home >Backend Development >C++ >C++ program to get the imaginary part of a given complex number
complex
class in C can represent
and controls several complex number operations. Let's take a look at how to represent and control
Show plural numbers.
As mentioned before, complex numbers are composed of real part and imaginary part.
To display the real part we use the real() function and the imag() function to display Given the imaginary part of a complex number. In the next example, we take a complex number number object, initialize it and then display the real and imaginary parts of the number respectively.//displaying the imaginary part only complex<double> c; cout << imag(c) << endl;
Get a new plural object.
Use the 'a.ib' notation to assign a value to the object.
Use the imag() function to display the imaginary part of a complex value.
#include <iostream> #include <complex> using namespace std; //displays the imaginary part of the complex number void display(complex <double> c){ cout << "The imaginary part of the complex number is: "; cout << imag(c) << endl; } //initializing the complex number complex<double> solve( double real, double img ){ complex<double> cno(real, img); return cno; } int main(){ complex<double> cno = 10.0 + 11i; //displaying the complex number cout << "The complex number is: " << real(cno) << '+' << imag(cno) << 'i' << endl; display(cno); return 0; }
The complex number is: 10+11i The imaginary part of the complex number is: 11
It should be noted that the imag() function only accepts complex numbers as input, and
Returns the imaginary part of the supplied complex number. The output value is
Template parameters of plural objects.
For many different operations across a wide range of scientific fields, complex
numbers are required. An interface for representing complex numbers is provided by the C complex class, which is part of the header fileThe above is the detailed content of C++ program to get the imaginary part of a given complex number. For more information, please follow other related articles on the PHP Chinese website!