Menu Close

Go – Program to subtract two numbers in go

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

Here, we are going to learn below things:

  • Subtract two integers¬†
  • Subtract two floating numbers
  • Subtract one integer and one floating number

Program to Subtract two integers:

Input: a = 20, b = 10
Ouput: 10 (20 - 10)

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

Code:

package main

import (

 "fmt"

)

func main() {

 var a, b int

 a = 20

 b = 10

 res := a - b

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

}

Output:

result: 10

Program to Subtract two floating numbers:

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

Input: a = 7.5, b = 2.5
Ouput: 5.00000
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: 7.100000

Program to Subtract 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: 6.800000

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.

Contact Us