Complex Number Program in C++
The primary purpose of a complex number program is to perform arithmetic operations and creating of data structures for complex arithmetic numbers. The real and imaginary parts of complex numbers are combined and are often expressed as "a + bi", where "a" is the real part and "b" for the imaginary part.
The Complex Number Program uses four different operations:
- Addition
- Subtraction
- Multiplication
- Division
Addition: The complex numbers (a + bi) and (c + di) of two have real and imaginary components added together one after the other.
Ex: (7+20i)+(56+29i)=(7+56)+(20+29)i
Subtraction: The two complex numbers (a + bi) and (c + di) are two having real and imaginary components and are subtracted one after the other.
Ex: (1 - 2i) + (2 - 4i) = (1 - 2) + (2 - 4)i
Multiplication: The multiplication multiplies two complex numbers (a + bi) and (c + di) with the help of distributive property.
Ex: (1 * 2i) + (2 * 4i) => ((1*2) - (2*4)) + ((1*4) - (2*2)) = (2 - 8) + (4 - 4)i
Division: The two complex integers (a + bi) and (c + di)are the denominator of the imaginary element of is eliminated by the help of Conjugate Denominators.
Ex: (1 / 2i) + (2 / 4i) => (((1*2)+(2*4))/(2^2 + 4^2)) + (((2*2)-(1*4))/(2^2 + 4^2))i
Program Code:
#include <iostream>
#include <cmath>
struct Complex
{
double a;
double b;
};
Complex add(const Complex& x, const Complex& y)
{
Complex result;
result.a=x.a+y.a;
result.b=x.b+y.b;
return result;
}
Complex subtract(const Complex& x, const Complex& y)
{
Complex result;
result.a=x.a-y.b;
result.b=x.b-y.b;
return result;
}
Complex multiply(const Complex& x, const Complex& y)
{
Complex result;
result.a=x.a*y.a-x.b*y.b;
result.b=x.a*y.b+x.b*y.a;
return result;
}
Complex divide(const Complex& x, const Complex& y)
{
Complex result;
double denominator=y.a*y.a+y.b*y.b;
result.a=(x.a*y.a+x.b*y.b)/denominator;
result.b=(x.b*y.a-x.a*y.b)/denominator;
return result;
}
double magnitude(const Complex& z)
{
return std::sqrt(z.a*z.a+z.b*z.b);
}
void display(const Complex& z)
{
std::cout<<z.a<<"+"<<z.b<<"i"<<std::endl;
}
int main()
{
Complex x={31.0,29.0};
Complex y={16.0,20.5};
Complex sum=add(x,y);
Complex diff=subtract(x,y);
Complex prod=multiply(x,y);
Complex div=divide(x,y);
std::cout<<"x:";
display(x);
std::cout<<"y:";
display(y);
std::cout<<"Sum:";
display(sum);
std::cout <<"Difference:";
display(diff);
std::cout<<"Product:";
display(prod);
std::cout<<"Division:";
display(div);
return 0;
}
Output:
![Complex Number Program in C++](https://static.tutorialandexample.com/cpp/complex-number-program-in-cpp1.png)
There are four different types of Complex Numbers, which are as follows:
- Imaginary Number
- Zero Complex Number
- Purely Imaginary Number
- Purely Real Number
Imaginary Number: In C++ program complex number functions, it is usually denoted by the imaginary number "i" or "j". The letter ‘i’ replaces the concept element ((-1)) with a fixed character. However, the letter ‘i’ is a built-in method in C++, so ‘j’ is often used when representing a conceptual entity in a complex number.
Zero Complex Number: In the C++ complex number system, a complex number is called a zero complex number if it shares zero real and imaginary elements. It is represented as a + bi. This comparison determines whether the real and imaginary properties of a complex number are within the zero. You can change the settings according to your desired accuracy.
Purely Imaginary Number: In C++ programming, a complex integer with a zero is a real element (0), and a non-zero imaginary part is referred to simply as a representation of "0 + bi" is labelled, and the imaginary element is replaced by "b". It is used to find pure imaginary numbers in your complex number system.
Please note that a simple quality check may not work in the right way when comparing complex integers due to floating-point arithmetic. For more accurate comparisons, consider choosing a lower value to address potential accuracy issues.
Purely Real Number: If the real factor is non zero and the imaginary part is zero, the complex number is called Purely Real Number in C++ complex number system. The equation is expressed as + 0i, where 'a' denotes the actual value.
Complex Number Program Using Structures in C++
#include <iostream>
using namespace std;
struct ComplexNumber {
double a;
double b;
};
ComplexNumber addComplexNumber(ComplexNumber p, ComplexNumber q) {
ComplexNumber result;
result.a=p.a+q.a;
result.b=p.b+q.b;
return result;
}
int main() {
ComplexNumber p, q, addition;
// Input First Complex Number
cout << "Enter Real Number and Imaginary Number parts of the First Complex Number: ";
cin >> p.a >> p.b;
// Input Second Complex Number
cout << "Enter Real Number and Imaginary Number parts of the Second Complex Number: ";
cin >> q.a >> q.b;
// Sum of the two complex numbers
addition = addComplexNumber(p, q);
cout << "Sum = " << addition.a << " + " << addition.b << "i" << endl;
return 0;
}
Output:
![Complex Number Program in C++](https://static.tutorialandexample.com/cpp/complex-number-program-in-cpp2.png)
Complex arithmetic functions that implement structures use basic arithmetic functions and other complex arithmetic functions. A system with independent members represents a complex imaginary (as b) and real number (as a).
Complex Structure
We need to create a program called Complex to represent complex numbers. The complex number itself and the imaginary block represent both aspects of the real and imaginary numbers.
Functions for Arithmetic Operations
We develop a collection of functions to perform simple mathematical operations on complex mathematical topics. These operations include addition, subtraction, multiplication, and division for complex calculations. The result of this operation accepting two complex integers as input is another complex number.
Function for Magnitude
There is also a more complex number function. By adding the square root of the square to the real and imaginary sides yields the complex number "a + bi".
Function for Display
For the clear report the complex number, we created a function that prints the complex numbers to the console in the standard "a + bi" format.
Main Function
In the main program we show how to use complex functions and numerical programs. Create complex integers "a" and "b" and apply arithmetic operations to them. The magnitude of the complex number is calculated, the result is printed, and the result is displayed using the demonstration function.