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
Golang Program to create Complex numbers from given imaginary parts using In-build Library Function
In this tutorial we will learn how to create Complex numbers from given imaginary parts in Go programming language.
A complex number is a basic data type of GoLang and is a combination of the real and imaginary part, where both parts of the complex number will be float type in golang.
Complex Number = Real Number + Imaginary Number
The function complex()in Go programming language is a built-in function that is used to construct a complex number from two floating-point values.
The real and imaginary parts of the complex value must be of the same size and the return value will be the corresponding complex type.
The values of real and imaginary parts are the parameters to construct a complex type number.
Example: Golang Program code to Create Complex Number From the given Imaginary Parts in one Single Function
Syntax
func complex(real, imaginary FloatType) ComplexType
Algorithm
Step 1 ? Import the package fmt
Step 2 ? Start function main().
Step 3 ? Construct the complex variable using the imaginary parts using Complex().
Step 4 ? Initialize the variable using the constructor init method
Step 5 ? Print the complex number on the screen using fmt.Println().
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 punctuation">(</span>
<span class="token string">"fmt"</span>
<span class="token punctuation">)</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 class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang Program to create Complex numbers from given imaginary parts"</span><span class="token punctuation">)</span>
<span class="token comment">// Constructing complex values</span>
<span class="token comment">// initializing the variable using the constructor init</span>
<span class="token comment">// using the function func complex(r, i FloatType) ComplexType</span>
c1 <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">complex</span><span class="token punctuation">(</span><span class="token number">7.25</span><span class="token punctuation">,</span> <span class="token number">51.8</span><span class="token punctuation">)</span>
c2 <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">complex</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
c3 <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">complex</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">10.5</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
c4 <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">complex</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">45.5</span><span class="token punctuation">)</span>
c5 <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">complex</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">11.45</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
<span class="token comment">// Printing the complex numbers from given imaginary parts</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 complex number of c1 is"</span><span class="token punctuation">,</span> c1<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 complex number of c2 is"</span><span class="token punctuation">,</span> c2<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 complex number of c3 is"</span><span class="token punctuation">,</span> c3<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 complex number of c4 is"</span><span class="token punctuation">,</span> c4<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 complex number of c15 is"</span><span class="token punctuation">,</span> c5<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>
Output
Golang Program to create Complex numbers from given imaginary parts The complex number of c1 is (7.25+51.8i) The complex number of c2 is (10+20i) The complex number of c3 is (-10.5+20i) The complex number of c4 is (1-45.5i) The complex number of c15 is (-11.45+5i)
Description of the Code
In the above program, we first declare the package main.
We imported the fmt package that includes the files of package fmt which provides the function to print anything.
We start the function main().GO program execution starts with the function main().
In the next line of code we have to create the complex number from its given imaginary parts.
Next initialize the variable by using the constructor init method with their real and imaginary values and call the Complex() function.
Last print the created complex number on the screen using the built-in function fmt.Println().
Conclusion
We have successfully compiled and executed example of the Golang code on how to create Complex numbers from given imaginary parts in Go programming language.
In this example we have used an in-built function Complex() to create the complex number.
