How to calculate the LCM (Least Common Multiple) of two numbers in Go

1 Answer

0 votes
package main

import "fmt"
 
func lcm(n1 int, n2 int) int { 
    var lcm int = 1
    if(n1 > n2)    {
        lcm = n1
    } else {
        lcm = n2
    }
    for {        
        if (lcm % n1 == 0 && lcm % n2==0) {   
            return lcm
        }
        lcm++
    }
}
 
 
func main() {
    n1 := 3
    n2 := 4
 
    fmt.Println(lcm(n1, n2))

}



/*
run:

12

*/

 



answered Aug 24, 2020 by avibootz
...