Home > Article > Backend Development > C++ program to initialize and print a complex number
To use the plural class, we must import the plural library, which can be done by importing or
Use the #include
double value1 = <double value>; double value2 = <double value>; complex <double> cno(value1, value2);
Accepts input in two numeric variables.
Pass these two variables to the constructor of the complex number.
Display plural numbers.
#include <iostream> #include <complex> using namespace std; //displays the complex number supplied void display(complex <double> c){ cout << "The complex number is: "; cout << real(c) << '+' << imag(c) << 'i' << endl; } //initializing the complex number complex<double> solve( double real, double img ){ complex<double> cno(real, img); return cno; } int main(){ //the real and the imaginary values are represented as double values double v1 = 10; double v2 = 7; //creating and displaying the complex number display(solve(v1, v2)); return 0; }
The complex number is: 10+7i
Any numeric data type can be used to replace the current
of a complex variable Type, double.We can also use the assignment operator to assign the values of the real and imaginary parts to a complex number number. However, to do this we must provide a number of type "a ib" and "b" are both numerical values. If the number is an integer, place zeros in the spaces after the decimal point. Decimal point must be used when writing real numbers Part "a". For example, 10 must be written as 10.0.
//the real and imaginary parts have to be assigned as it is complex <double> cno = 15.0 + 6i;
Get a new plural object.
Assign a value to the object using 'a.ib' notation.
Display plural values.
#include <iostream> #include <complex> using namespace std; //displays the complex number supplied void display(complex <double> c){ cout << "The complex number is: "; cout << real(c) << '+' << imag(c) << 'i' << endl; } int main(){ //initializing a complex number object complex <double> cno = 15. + 6i; //displaying the complex number display(cno); return 0; }
The complex number is: 15+6i
Using the "real()" and "imag()" functions, you can get the real and imaginary parts of complex integers.
are displayed individually. The "real()" function displays the real part of a complex number, The "imag()" function displays the imaginary part of a complex number. Here is an examplesample.
//displaying in the a + ib format complex<double> c; cout << real(c) << '+' << imag(c) << 'i' << endl;
Get a new plural object.
Assign a value to an object using 'a.ib' notation.
Display plural values.
#include <iostream> #include <complex> using namespace std; //displays the complex number supplied void display(complex <double> c){ cout << "The complex number is: "; cout << real(c) << '+' << imag(c) << 'i' << endl; } //initializing the complex number complex<double> solve( double real, double img ){ complex<double> cno(real, img); return cno; } int main(){ //the real and the imaginary values are represented as double values double v1 = 3; double v2 = 4; //creating and displaying the complex number display(solve(v1, v2)); return 0; }
The complex number is: 3+4i
Complex numbers are needed in a wide range of applications in many different programs.
scientific disciplines. The C plural class, included in header fileThe above is the detailed content of C++ program to initialize and print a complex number. For more information, please follow other related articles on the PHP Chinese website!