Go 程序查找数字的因数

这个 Go 程序通过一个从 1 开始迭代到 n 的 for 循环来查找数字的因数。If 语句 (factorsnum%i == 0) 检查 factorsNum 和 I 值的余数是否等于零。如果为 True,则打印该因数。

package main

import "fmt"

func main() {

    var factorsnum, i int

    fmt.Print("Enter any Number to find Factors = ")
    fmt.Scanln(&factorsnum)

    fmt.Println("The Factors of the ", factorsnum, " are = ")
    for i = 1; i <= factorsnum; i++ {
        if factorsnum%i == 0 {
            fmt.Println(i)
        }
    }
}
Go Program to Find Factors of a Number

Golang 程序使用函数查找数字的因数。

package main

import "fmt"

func findFactors(factorsnum int) {
    for i := 1; i <= factorsnum; i++ {
        if factorsnum%i == 0 {
            fmt.Println(i)
        }
    }
}
func main() {

    var factorsnum int

    fmt.Print("Enter any Number to find Factors = ")
    fmt.Scanln(&factorsnum)

    fmt.Println("The Factors of the ", factorsnum, " are = ")
    findFactors(factorsnum)
}
SureshMac:Goexamples suresh$ go run factors2.go
Enter any Number to find Factors = 50
The Factors of the  50  are = 
1
2
5
10
25
50
SureshMac:Goexamples suresh$ go run factors2.go
Enter any Number to find Factors = 76
The Factors of the  76  are = 
1
2
4
19
38
76