C++ Program to initialize and print a complex number


Complex numbers are a very fundamental concept in modern science, the concept is first introduced in the early 17th century. Complex numbers are of the form a + ib, where a and b are real numbers. a is known as the real part and ib is known as the imaginary part of a complex number. In C++, there is a class to represent complex numbers which is the complex class. The complex class in C++ can represent and manipulate various operations on complex numbers. We take a look at how to represent, initialize, and display complex numbers.

Initializing using the class constructor

To use the complex class, we have to import the complex library which can be imported or included using the #include statement. The declaration and the initialization of a complex object are described below. In the first example, we use the constructor to assign values to the real and the imaginary part of the complex number as initialization.

Syntax

double value1 = <double value>;
double value2 = <double value>;
complex <double> cno(value1, value2);

Algorithm

  • Take input in two numerical variables.

  • Pass the two variables to the constructor of the complex number.

  • Display the complex number.

Example

#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;
}

Output

The complex number is: 10+7i

Any numerical data type can be used in place of the complex number variable's current type, which is double.

Initializing using the assignment operator

We may also use the assignment operator to assign real and imaginary values to a complex number. Nevertheless, to do that, we must provide a number of the type "a + ib," where "a" and "b" are both numerical values. If the number is an integer, zeros are placed in the space after the decimal points. A decimal point must be used when writing the real portion "a." 10 must be written as 10.0, for instance.

Syntax

//the real and imaginary parts have to be assigned as it is
complex <double> cno = 15.0 + 6i;

Algorithm

  • Take a new complex number object.

  • Assign a value to the object using the ‘a. + ib’ notation.

  • Display the complex number value.

Example

#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;
}

Output

The complex number is: 15+6i

Displaying a complex number

Using the "real()" and "imag()" functions, the real and imaginary parts of a complex integer are displayed separately. The "real()" function shows the complex number's real portion, whereas the "imag()" function shows the complex number's imaginary portion. Here is a sample of that.

Syntax

//displaying in the a + ib format
complex<double> c;
cout << real(c) << '+' << imag(c) << 'i' << endl;

Algorithm

  • Take a new complex number object.

  • Assign the value to the object using the ‘a. + ib’ notation.

  • Display the complex number value.

Example

#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;
}

Output

The complex number is: 3+4i

Conclusion

Complex numbers are needed for many different procedures across a wide range of scientific disciplines. The C++ complex class, included in the header , offers an interface for representing complex numbers. The complex class supports all operations on complex numbers, including addition, subtraction, multiplication, conjugation, norm, and numerous more operations. As we've already discussed in this piece, it's not too difficult to change conventional numerical values into complex numbers. We have to note that while displaying complex numbers, we have to take care of the real part and the imaginary part of the number, otherwise it may generate several errors in the program.

Updated on: 07-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements