find the least common multiple of two numbers in go

Here's an example code in Go to find the LCM of two numbers using the Euclidean algorithm:

main.go
package main

import "fmt"

func main() {
   var a, b, gcd, lcm int

   fmt.Print("Enter two positive integers: ")
   fmt.Scanln(&a)
   fmt.Scanln(&b)

   // Calculate the greatest common divisor (GCD)
   for i := 1; i <= a && i <= b; i++ {
      if a % i == 0 && b % i == 0 {
          gcd = i
      }
   }

   // Calculate the least common multiple (LCM)
   lcm = (a*b) / gcd

   fmt.Printf("The LCM of %d and %d is %d", a, b, lcm)
}
436 chars
24 lines

In this example, we first take input for two positive integers. After that, we calculate the GCD of the input integers using a loop that checks for every integer between 1 and the input numbers to find the greatest common divisor. Finally, we use the formula lcm = (a*b) / gcd to calculate the LCM of two numbers.

gistlibby LogSnag