Files
  • main.go
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import "fmt"

func main() {

	var n int
	var num int
	var result int

	fmt.Println("How many integers GCD do you want to calculate?")
	fmt.Scanf("%d\n", &n)
	fmt.Println("Enter space separated integers:")
	numList := map[int]int{}

	for i := 1; i <= n; i++ {
		fmt.Scanf("%d", &num)
		numList[i] = num
	}
	//This is the result for only 2 integers
	result = gcd(numList[1], numList[2])

	//for loop in case there're more than 2 ints
	for j := 3; j <= n; j++ {
		result = gcd(result, numList[j])
	}

	fmt.Println("The GCD/HCF of given integers is: ", result)
}

//Func to implement Euclid Algo
func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}
go version go1.9.4 linux/amd64