Golang Program to get the magnitude of the given number

In this article we will discuss about how to get the magnitude of a number in Go language.

Magnitude of a quantity is defined as its numeric value. Magnitude of a number is always positive. In this article we will discuss about different methods by which we can obtain the magnitude of any number.

Syntax

func Abs(number float64) float64

Abs() is a method defined in math library. This method accepts a 64 bit float number as an argument and return the absolute value of it excluding the sign.

The source code to the above stated problem is compiled and executed below.

Example 1

The simplest way to obtain the magnitude of any integer in go language is by using a library function Abs(). This function returns the absolute value of an integer.

Algorithm

  • Step 1 ? Import the package fmt.

  • Step 2 ? start the main function().

  • Step 3 ? Initialize a variable of data type int and store value in it.

  • Step 4 ? call the abs() function defined in math package and store the result.

  • Step 5 ? print the results on the screen.

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 comment">// golang program to get the magnitude of a number</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"math"</span>
<span class="token punctuation">)</span>

<span class="token comment">// fmt package allows us to print anything on the screen</span>
<span class="token comment">// math package allows us to use mathematical methods in go language.</span>
<span class="token comment">// calling the main() function</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">// initializing a variable number and storing a value in it.</span>
   <span class="token keyword">var</span> number float64 <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">3.8</span>
   
   <span class="token comment">// calling the absolute method defined in math package</span>
   result <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">math<span class="token punctuation">.</span></span>Abs</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
   
   <span class="token comment">// printing the result on the screen.</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">"Magnitude of:"</span><span class="token punctuation">,</span>number<span class="token punctuation">,</span><span class="token string">"is "</span><span class="token punctuation">,</span> result<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

<font face="Liberation Mono, Consolas, Menlo, Courier, monospace"><span style="font-size: 14px;">Magnitude of: -3.8 is  3.8</span></font>

Description of the Code

  • First, we import the package fmt that allows us to print anything and math package to use Abs() method.

  • Then we call the main() function.

  • Now we need to get the number whose magnitude we wish to calculate.

  • Pass this number in Abs() method defined in math package and store the result in a separate variable.

  • Abs() is a method defined in math package that takes a float number as an argument and returns the numeric value of the number (excluding the sign).

  • Print the results on the screen using fmt.Println() function.

Example 2

Now there is one more way by which we can obtain the magnitude of a number. This method includes creating our own logic to implement the result.

Algorithm

  • Step 1 ? Import the package fmt.

  • Step 2 ? start the main function().

  • Step 3 ? Initialize a variable and store values in it.

  • Step 4 ? Implement the logic and store the result.

  • Step 5 ? print the results on the screen.

Example

The source code for that is compiled and executed below.

<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 keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"math"</span>
<span class="token punctuation">)</span>

<span class="token comment">// fmt package allows us to print anything on the screen.</span>
<span class="token comment">// math package allows us to use mathematical methods in go language.</span>
<span class="token comment">// creating and defining the magnitude function.</span>
func magnitude <span class="token punctuation">(</span>number float64<span class="token punctuation">)</span> float64 <span class="token punctuation">{</span>

   <span class="token comment">// initializing a temp variable to store the value of square</span>
   <span class="token keyword">var</span> temp float64
   
   <span class="token comment">// implementing the logic to get the magnitude.</span>
   <span class="token comment">// here we are using the logic from definition of magnitude of a</span>
   <span class="token comment">// number which says magnitude of a number is the root of its square.</span>
   <span class="token comment">// finding the square of the given number using pow() method defined</span>
   <span class="token comment">// in math library</span>
   <span class="token comment">// then storing the result of square in a temp variable</span>
   temp <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">math<span class="token punctuation">.</span></span>Pow</span><span class="token punctuation">(</span>number<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
   
   <span class="token comment">// finding the root of the above obtained result and storing the</span>
   <span class="token comment">// final answer in result variable.</span>
   result <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">math<span class="token punctuation">.</span></span>Pow</span><span class="token punctuation">(</span>temp<span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">)</span>
   
   <span class="token comment">// returning the final result</span>
   <span class="token keyword">return</span> result
<span class="token punctuation">}</span>
<span class="token comment">// calling the main() function</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">// initializing a variable number.</span>
   <span class="token keyword">var</span> number float64
   
   <span class="token comment">// assigning value to the number</span>
   number <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">8.9</span>
   
   <span class="token comment">// calling the magnitude() function and passing the number in it</span>
   result <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">magnitude</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
   
   <span class="token comment">// printing the result on the screen.</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">"Magnitude of:"</span><span class="token punctuation">,</span>number<span class="token punctuation">,</span><span class="token string">"is "</span><span class="token punctuation">,</span> result<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

Magnitude of: -3.8 is  3.8

Description of the Code

  • First, we import the package fmt that allows us to print anything and math package to use Pow() method defined in the math package.

  • Then we create and define the magnitude() function which will contain our logic to find the magnitude of the number.

  • Then we call the main() function.

  • Now we need to get the number whose magnitude we wish to calculate.

  • Call the magnitude function by passing the number in it as an argument.

  • To find the magnitude we are using the standard mathematical definition of magnitude which says magnitude of a number is the root of its square.

  • To find the square of a number in golang we are using math.Pow() method. This method takes two arguments one is the number whose power we need to raise and second is the number of times we want to raise the power for example: to find the square of a number using this function the code will be math.Pow(number, 2).

  • Once the square is calculated we need to store the result to a variable which in our case is temp and use the similar approach to find the root of the function. To find the root of a number using math.Pow() method in golang the code will be math.Pow(number, 0.5).

  • Once we have achieved the magnitude we need to return this value back.

  • We have then stored the final result in the result variable.

  • Then we print the results on the screen using fmt.Println() function.

Conclusion

We have successfully compiled and executed the Go language program that will get us the magnitude of any number along with examples using both library and user defined functions.

Updated on: 2022-11-14T12:51:31+05:30

878 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements