- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to convert a number into a complex number

Complex numbers in C++ are available in the header <complex> and it enables a developer to represent and manipulate complex numbers. Complex numbers have the form a + ib, where 'a' is referred to as the complex number's real portion and 'ib' is the number's imaginary part. The imaginary portion is denoted by the letter 'i' whose value is "iota" which is equal to -1. Complex numbers are extremely important in many programs since they are employed in many mathematical and scientific processes. We take a look at how to represent complex numbers in C++ and how to convert a normal number to a complex number.

## Using the Constructor

We can construct a complex number using the constructor of the class complex. To create a complex number, we have to pass the real and imaginary parts of the number as arguments to the constructor.

### 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; } int main(){ //the real and the imaginary values are represented as double values double value1 = 2.05; double value2 = 3; //creating the complex number complex <double> cno(value1, value2); display(cno); return 0; }

### Output

The complex number is: 2.05+3i

We have taken the variable type of the complex numbers as double, but any numerical data type can be used in place of that.

## Using the assignment operator

We can also use the assignment operator to assign real and imaginary values to a complex number. But, to assign that we have to assign the number in the form ‘a + bi’, where a and b are numerical values. The real part ‘a’ has to be written with a decimal point; if the number is an integer, we fill the part after the decimal points with zeroes. For example, we have to write 5 as 5.0.

### Syntax

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

### 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; } int main(){ //creating the complex number complex <double> cno = 5.0 + 2i; display(cno); return 0; }

### Output

The complex number is: 5+2i

## Displaying a complex number

The real and the imaginary part of a complex number has to be displayed differently using the ‘real()’ and ‘ imag()’ functions. The ‘real()’ function displays the real part of the complex number whereas the ‘imag()’ function represents the imaginary part of the complex number. We see an example of that.

### Syntax

//displaying in the a + ib format 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; } int main(){ //creating the complex number complex <double> cno = 7.0 + 9i; display(cno); return 0; }

### Output

The complex number is: 7+9i

### Conclusion

Complex numbers are very much needed in various operations in various scientific fields. The complex class in C++ provides that interface to represent complex numbers. The complex class supports all types of operations on complex numbers such as addition, subtraction, multiplication, conjugation, norm, and much more. The conversion from normal numerical values to complex numbers is very easy as we have discussed in this article.