find the remainder of two numbers in go

To find the remainder of two numbers in Go, we can use the modulus operator (%). The modulus operator returns the remainder of the division operation between two numbers.

Here's an example:

main.go
package main

import "fmt"

func main() {
    var a, b int
    fmt.Print("Enter two numbers: ")
    fmt.Scanln(&a, &b)

    remainder := a % b
    fmt.Println("Remainder:", remainder)
}
186 chars
13 lines

In this example, we first declare two integer variables a and b. We then prompt the user to enter two numbers using fmt.Scanln() and store them in a and b.

We then calculate the remainder using the modulus operator % and store it in a new variable remainder. Finally, we print the result using fmt.Println().

Note that the modulus operator can be used with floating-point numbers as well, but the result may not be accurate due to floating-point precision issues.

related categories

gistlibby LogSnag