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

package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the variables of type float var num1, num2, num3 float32 // initializing the variables which we have to multiply num1 = 25.5 num2 = 23.333 // multiplying the float variables and storing them into a third variable num3 = num1 * num2 // printing the multiplication of two numbers fmt.Println("Multiplication of", num1, "and", num2, "=\n", num3,"\n(multiplying within the function)") }

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

package main // fmt package provides the function to print anything import "fmt" func multiplyFloatNumber(num1, num2 float32) float32 { return num1 * num2 } func main() { // declaring the variables of type float var num1, num2, num3 float32 // initializing the variables which we have to multiply num1 = 25.5 num2 = 23.333 // multiplying the float variables and storing them into a third variable num3 = multiplyFloatNumber(num1, num2) // printing the result fmt.Println("Multiplication of", num1, "and", num2, "=\n", num3, "\n(multiplying outside the function)") }

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: 26-Aug-2022

719 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements