Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,948 questions

51,890 answers

573 users

How to calculate the GCD (greatest common divisor) of two integers in Go

1 Answer

0 votes
package main

import "fmt"
 
func gcd(n1 int, n2 int) int {
    var gcd int

    for i := 1; i <= n1 && i <= n2 ; i++ {
        if (n1 % i == 0 && n2 % i == 0) {
            gcd = i
        } 
    }
    return gcd
}  
 
 
func main() {
    n1 := 24
    n2 := 54
 
    fmt.Println(gcd(n1, n2))

}



/*
run:

6

*/

 



answered Aug 24, 2020 by avibootz
...