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("Enter the number: ")
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)
}输出
Enter the number: 75 The smallest divisor of the number is: 3
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP