C++: Complex Number Arithmetic


A complex number is a number which can be written as “a + b \times i” (sometimes shown as “b + a \times i“) where a and b are real numbers and i is the square root of -1. Typically, complex numbers are represented as a pair of real numbers called the “imaginary part” and “real part”, where the imaginary part is the number to be multiplied by i.

  • Show addition, multiplication, negation, and inversion of complex numbers in separate functions. (Subtraction and division operations can be made with pairs of these operations.) Print the results for each operation tested.
  • Optional: Show complex conjugation. By definition, the complex conjugate of a + bi is abi.

Some languages have complex number libraries available. If your language does, show the operations. If your language does not, also show the definition of this type.

#include <iostream>
#include <complex>
using std::complex;

void complex_operations() {
	complex<double> a(1.0, 1.0);
	complex<double> b(3.14159, 1.25);

	// addition
	std::cout << a + b << std::endl;
	// multiplication
	std::cout << a * b << std::endl;
	// inversion
	std::cout << 1.0 / a << std::endl;
	// negation
	std::cout << -a << std::endl;
	// conjugate
	std::cout << std::conj(a) << std::endl;


Content is available under GNU Free Documentation License 1.2.