How to Multiply Two Floating-Point Numbers in Golang?

This tutorial will show how to multiply two float numbers within a function or by creating a separate function and calling it in the current function.

Multiplying two float numbers within the function

Algorithm

  • STEP 1 ? Defining the floating variables that we want to multiply and the floating variable in which we will add the result.

  • STEP 2 ? Initialize the variables with the respective values you want to multiply.

  • STEP 3 ? Multiply two numbers and store them in the third variable.

  • STEP 4 ? Print the result after multiplying two 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 variables of type float</span>
   <span class="token keyword">var</span> num1<span class="token punctuation">,</span> num2<span class="token punctuation">,</span> num3 float32
   
   <span class="token comment">// initializing the variables which we have to multiply</span>
   num1 <span class="token operator">=</span> <span class="token number">25.5</span>
   num2 <span class="token operator">=</span> <span class="token number">23.333</span>
   
   <span class="token comment">// multiplying the float variables and storing them into a third variable</span>
   num3 <span class="token operator">=</span> num1 <span class="token operator">*</span> num2
   
   <span class="token comment">// printing the multiplication of two numbers</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">"Multiplication of"</span><span class="token punctuation">,</span> num1<span class="token punctuation">,</span> <span class="token string">"and"</span><span class="token punctuation">,</span> num2<span class="token punctuation">,</span> <span class="token string">"=\n"</span><span class="token punctuation">,</span> num3<span class="token punctuation">,</span><span class="token string">"\n(multiplying within the function)"</span><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 function first, we are declaring three float type variables. Then we are initializing two of them with the floating numbers we want to multiply. In this step, we are multiplying the numbers and storing them in the third number. In the end, we are printing the result.

Output

Multiplication of 25.5 and 23.333 =
594.9915
(multiplying within the function)

Multiplying two float numbers outside the function

Algorithm

  • STEP 1 ? Defining the floating variables that we want to multiply and the floating variable in which we will add the result.

  • STEP 2 ? Initialize the variables with the respective values you want to multiply.

  • STEP 3 ? Multiply two numbers by calling multiplyFloatNumber() function and storing them in the third variable.

  • STEP 4 ? Print the result after multiplying two 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">multiplyFloatNumber</span><span class="token punctuation">(</span>num1<span class="token punctuation">,</span> num2 float32<span class="token punctuation">)</span> float32 <span class="token punctuation">{</span>
   <span class="token keyword">return</span> num1 <span class="token operator">*</span> num2
<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 comment">// declaring the variables of type float</span>
   <span class="token keyword">var</span> num1<span class="token punctuation">,</span> num2<span class="token punctuation">,</span> num3 float32
   
   <span class="token comment">// initializing the variables which we have to multiply</span>
   num1 <span class="token operator">=</span> <span class="token number">25.5</span>
   num2 <span class="token operator">=</span> <span class="token number">23.333</span>
   
   <span class="token comment">// multiplying the float variables and storing them into a third variable</span>
   num3 <span class="token operator">=</span> <span class="token function">multiplyFloatNumber</span><span class="token punctuation">(</span>num1<span class="token punctuation">,</span> num2<span class="token punctuation">)</span>
   
   <span class="token comment">// printing the result</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">"Multiplication of"</span><span class="token punctuation">,</span> num1<span class="token punctuation">,</span> <span class="token string">"and"</span><span class="token punctuation">,</span> num2<span class="token punctuation">,</span> <span class="token string">"=\n"</span><span class="token punctuation">,</span> num3<span class="token punctuation">,</span> <span class="token string">"\n(multiplying outside the function)"</span><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 function first, we are declaring three float type variables. Then we are initializing two of them with the floating numbers we want to multiply. In this step, we are multiplying the numbers by calling multiplyFloatNumber() function and storing them in the third number. In the end, we are printing the result.

Description of multiplyFloatNumber(num1, num2 float32) float32.

  • (num1, num2 float32) - This is the way in Golang to create a function as parameters. In this function, we have two parameters of float32 type.

  • func multiplyFloatNumber(num1, num2 float32) float32 - The float32 at the end of a function definition is showing the return type of the function which is float 32 for this function.

Output

Multiplication of 25.5 and 23.333 =
594.9915
(multiplying outside the function)

In these ways, we can multiply to float numbers. To make code more readable and modular we can go with the second approach. As, on giving the appropriate name to the function, one who does not know the respective programming language that person can also understand the code. This is all about this article to learn more about Golang you can refer to these tutorials.

Updated on: 2022-08-26T08:04:03+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements