Menu Close

Go – Program to multiply two numbers in go

Here, we will write a program to multiply two numbers in go. Given below, you can find algorithm and program.

Here, we are going to learn below things:

  • Multiply two integers 
  • Multiply two floating numbers
  • Multiply one integer and one floating number

Program to Multiply two integers:

Input: a = 10, b = 20
Ouput: 30 (10 * 20)

Input: a = 2, b = 5
Ouput: 10

Code:

package main

import (

 "fmt"

)

func main() {

 var a, b int

 a = 10

 b = 20

 res := a * b

 fmt.Printf("result: %d\n", res)

}

Output:

result: 200

Program to Multiply two floating numbers:

Input: a = 10.3, b = 3.2
Ouput: 32.960000

Input: a = 2.2, b = 2.5
Ouput: 5.500000
package main

import (

 "fmt"

)

func main() {

 var a, b float64

 a = 10.3

 b = 3.2

 res := a * b

 fmt.Printf("result: %f\n", res)

}

Output:

result: 32.960000

Program to Multiply one integer and one floating number:

Input: a = 10, b = 3.2
Ouput: 32.000000

Input: a = 10, b = 2.5
Ouput: 25.00000

Code:

package main

import (

 "fmt"

)

func main() {

  var a int
  var b float64

 a = 10

 b = 3.2

 res := float64(a) * b

 fmt.Printf("result: %f\n", res)

}

Output:

result: 32.000000

To learn more about golang. Please follow given below link.

https://www.techieindoor.com/go-lang/

References:

https://golang.org/doc/
https://golang.org/pkg/

Posted in golang, golang program

Leave a Reply

Your email address will not be published. Required fields are marked *