<complex> header file is used for declaring a complex number in C++.
We can declare the complex number by using complex(3,4) where 3 is a real number and 4 is imaginary part.
There are three real types in complex numbers. They are float complex, double complex, long double complex.
This section on C++ interview questions and answers focuses on “Complex Number Type”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams.
These questions can be attempted by anyone focusing on learning C++ programming language.
They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.
Examples:
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <complex> using namespace std; int main() { complex<double> c1(4.0, 16.0), c2; c2 = pow(c1, 2.0); cout << c2; return 0; } |
Output: In this program, we are finding the square of the complex number.
1 2 3 |
<span class="br0">(</span><span class="sy2">-</span><span class="nu0">240</span>,<span class="nu0">128</span><span class="br0">)</span> |
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <complex> using namespace std; int main() { complex<double> c_double(2, 3); complex<int> c_int(4, 5); c_double *= 2; c_double = c_int; cout << c_double; return 0; } |
Output: We are just copying the value of c_int into c_double, So it’s printing as (4,5).
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <complex> using namespace std; int main() { complex<int> i(2, 3); i = i * 6 / 3; cout << i; return 0; } |
Output: We are multiplying the complex number by 2.
1 2 3 |
<span class="br0">(</span><span class="nu0">4</span>,<span class="nu0">6</span><span class="br0">)</span> |
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <complex> using namespace std; int main() { complex<double> c1(4.0,3.0); cout << "c1: " << c1; complex<float> c2(polar(5.0,0.75)); cout << c1 + complex<double>(c2.real(),c2.imag()); return 0; } |
Output: We are adding the two complex numbers and printing the result.
1 2 3 |
c1<span class="sy4">:</span> <span class="br0">(</span><span class="nu0">4</span>,<span class="nu0">3</span><span class="br0">)</span><span class="br0">(</span><span class="nu16">7.65844</span>,<span class="nu16">6.40819</span><span class="br0">)</span> |
C++Code:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <complex> using namespace std; int main() { complex<double> c1(4.0, 3.0); complex<float> c2(polar(5.0, 0.75)); cout << (c1 += sqrt(c1)) << endl; return 0; } |
Output: In this program, we are adding both complex number and finding the square root of it.
1 2 3 |
<span class="br0">(</span><span class="nu16">6.12132</span>,<span class="nu16">3.70711</span><span class="br0">)</span> |
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <complex> using namespace std; int main () { complex<double> mycomplex (20.0, 2.0); cout << imag(mycomplex) << endl; return 0; } |
Output: imag part will return the imaginary part of the complex number.
1 2 3 |
<span class="nu0">2</span> |