寻找整数的最小除数的 Golang 程序
考虑整数是:75
该整数的除数是:3,5,15,...,75
最小的除数是:3
脚步
从用户那里取一个整数。
使用该数字初始化变量(res)。
使用for循环,其中i的值范围从2到整数。
如果数字能被i整除,则与res进行比较。如果res>i,然后用i更新res。
退出循环并打印res。
示例
package main
import "fmt"
func main(){
var n int
fmt.Print("输入号码: ")
fmt.Scanf("%d", &n)
res := n
for i:=2; i<=n; i++{
if n%i == 0{
if i<=res{
res=i
}
}
}
fmt.Printf("The smallest divisor of the number is: %d", res)
}输出结果输入号码: 75 The smallest divisor of the number is: 3
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短