Finding Mod of Given Number in Golang


In mathematics, the modulo operation is used to determine the remainder of a division operation. In Golang, we can find the modulo of a given number using the % operator. In this article, we will discuss how to find the mod of a given number in Golang.

In mathematics, the modulo operation is used to determine the remainder of a division operation. In Golang, we can find the modulo of a given number using the % operator. In this article, we will discuss how to find the mod of a given number in Golang.

Finding Mod of Given Number in Golang

To find the mod of a given number in Golang, we can use the % operator. The % operator returns the remainder of the division operation. Let's take a look at the following example −

Example

package main

import "fmt"

func main() {
   a := 10
   b := 3
   fmt.Println(a % b)
}

Output

1

In this example, we declared two variables a and b with the values of 10 and 3, respectively. We then printed the result of a % b, which is 1.

We can also find the mod of floating-point numbers using the math.Mod() function from the math package. The math.Mod() function takes two arguments: a and b. It returns the floating-point remainder of a divided by b. Let's take a look at the following example −

Example

package main

import (
   "fmt"
   "math"
)

func main() {
   a := 10.5
   b := 2.2
   fmt.Println(math.Mod(a, b))
}

Output

1.6999999999999993

In this example, we declared two floating-point variables a and b with the values of 10.5 and 2.2, respectively. We then printed the result of math.Mod(a, b), which is 1.6999999999999993.

Conclusion

In this article, we discussed how to find the mod of a given number in Golang. We can find the mod of an integer using the % operator, and the mod of a floating-point number using the math.Mod() function from the math package. I hope you found this article helpful!

Updated on: 12-Apr-2023

846 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements