Articles → CSHARP → Perform The Mathematical Operations On The Complex Numbers In C#

Perform The Mathematical Operations On The Complex Numbers In C#






Addition




using System.Numerics;

Complex a = new Complex(2, 3); // complex number 2+3i
Complex b = new Complex(4, -1); // complex number 4-1i

Complex c = a + b; // add complex numbers a and b

Console.WriteLine("Result After addition of 2 complex Numbers:-");
Console.WriteLine(c); // output: (6, 2)

Console.ReadLine();


Picture showing the output of adding 2 complex numbers
Click to Enlarge


Subtracting




using System.Numerics;

Complex a = new Complex(2, 3); // complex number 2+3i
Complex b = new Complex(4, -1); // complex number 4-1i

Complex c = a - b; // subtract complex numbers a and b

Console.WriteLine("Result After subtracting of 2 complex Numbers:-");
Console.WriteLine(c);

Console.ReadLine();


Picture showing the output of subtracting 2 complex numbers
Click to Enlarge


Multiplication




using System.Numerics;

Complex a = new Complex(2, 3); // complex number 2+3i
Complex b = new Complex(4, -1); // complex number 4-1i

Complex c = a * b; // multiply complex numbers a and b

Console.WriteLine("Result After multiplying 2 complex Numbers:-");
Console.WriteLine(c);

Console.ReadLine();


Picture showing the output of multiplicating 2 complex numbers
Click to Enlarge


Division




using System.Numerics;

Complex z1 = new Complex(2, 3); // first complex number
Complex z2 = new Complex(4, -5); // second complex number
Complex result = Complex.Divide(z1, z2); // divide z1 by z2
Console.WriteLine("Result After dividing 2 complex Numbers:-");
Console.WriteLine(result); // display the result


Console.ReadLine();


Picture showing the output of dividing 2 complex numbers
Click to Enlarge




Posted By  -  Karan Gupta
 
Posted On  -  Saturday, May 13, 2023

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250