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