- 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 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
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