Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add two Complex numbers in Golang?
In this tutorial, we will learn how to declare and add complex numbers in Golang. Complex numbers are something that has an imaginary and real part which will make them different from other types of numbers. Golang supports declaring a variable of complex number type.
Complex Number = Real Number + Imaginary Number
Different ways to declare and initialize complex numbers and add them later in Golang.
Initializing using complex number init syntax
Syntax
var variableName complex64 var variableName complex128 variableName = (realValue) + (imaginaryValue)i
Algorithm:
STEP 1 ? Defining the complex variables that we want to add and the complex variable in which we will add the result.
STEP 2 ? Initialize the variables with the respective values you want to add.
STEP 3 ? Add two numbers and store them in the third variable.
STEP 4 ? Print the result after adding two complex numbers.
Example
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the complex number using the var keyword</span>
<span class="token keyword">var</span> complexNumber1<span class="token punctuation">,</span> complexNumber2<span class="token punctuation">,</span> complexNumber3 complex64
<span class="token comment">// initializing the variable using complex number init syntax</span>
complexNumber1 <span class="token operator">=</span> <span class="token number">3</span> <span class="token operator">+</span> <span class="token number">3</span>i
complexNumber2 <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">5</span>i
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The First Complex Number is"</span><span class="token punctuation">,</span> complexNumber1<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The Second Complex Number is"</span><span class="token punctuation">,</span> complexNumber2<span class="token punctuation">)</span>
<span class="token comment">// adding the complex numbers using + operator</span>
complexNumber3 <span class="token operator">=</span> complexNumber1 <span class="token operator">+</span> complexNumber2
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Printing the addition of two complex numbers by initializing the variable using complex number init syntax."</span><span class="token punctuation">)</span>
<span class="token comment">// printing the complex number after addition</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span>complexNumber1<span class="token punctuation">,</span> <span class="token string">"+"</span><span class="token punctuation">,</span> complexNumber2<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span> complexNumber3<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
In the above code first, we are declaring the complex numbers then we are initializing two of them using complex number init syntax with their real and imaginary values. After that, we are adding the two complex numbers and store the result in the third variable, and then print it.
Output
The First Complex Number is (3+3i) The Second Complex Number is (2+5i) Printing the addition of two complex numbers by initializing the variable using complex number init syntax. (3+3i) + (2+5i) = (5+8i)
Initializing using the Constructor
In this way, we are using the constructor to initialize the complex number variable you have to just pass the real and the imaginary value.
Syntax
var variableName complex64 var variableName complex128 variableName = complex(realValue, imaginaryValue)
Example
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the complex number using the var keyword</span>
<span class="token keyword">var</span> complexNumber1<span class="token punctuation">,</span> complexNumber2<span class="token punctuation">,</span> complexNumber3 complex64
<span class="token comment">// initializing the variable using the constructor</span>
complexNumber1 <span class="token operator">=</span> <span class="token function">complex</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
complexNumber2 <span class="token operator">=</span> <span class="token function">complex</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The First Complex Number is"</span><span class="token punctuation">,</span> complexNumber1<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The Second Complex Number is"</span><span class="token punctuation">,</span> complexNumber2<span class="token punctuation">)</span>
<span class="token comment">// adding the complex numbers using + operator</span>
complexNumber3 <span class="token operator">=</span> complexNumber1 <span class="token operator">+</span> complexNumber2
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Printing the addition of two complex numbers by initializing the variable using the constructor."</span><span class="token punctuation">)</span>
<span class="token comment">// printing the complex number after addition</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span>complexNumber1<span class="token punctuation">,</span> <span class="token string">"+"</span><span class="token punctuation">,</span> complexNumber2<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span> complexNumber3<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
In the above code first, we are declaring the complex numbers then we are initializing two of them using the constructor with their real and imaginary values. After that, we are adding the two complex numbers and store the result in the third variable, and then print it.
Output
The First Complex Number is (5+4i) The Second Complex Number is (6+3i) Printing the addition of two complex numbers by initializing the variable using the constructor. (5+4i) + (6+3i) = (11+7i)
These are the two ways to initialize the complex number and then add them. To learn more about Golang you can explore this tutorials.
