Golang 程序以统计数字个数
假设这个数字为:123456
提供的数字中的位数:6
为了统计一个数字中的位数,我们可以采取以下
步骤
- 获取整数的值并将其存储在变量中。
- 使用 while 循环获取数字的每位数字,并在每次获取到数字时增加计数。
- 打印给定整数的位数。
示例
package main import "fmt" func main(){ var n int fmt.Print("Enter the number: ") fmt.Scanf("%d", &n) count := 0 for n >0 { n = n/10 count++ } fmt.Printf("The number of digits in the given number is: %d", count) }
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
Enter the number: 123456 The number of digits in the given number is: 6
广告