Golang : Count number of digits from given integer value
Just a simple program to count number of digits from a given integer value. Need this to calculate if an integer value generated by another function has 1 digit, 2 digits or more. If 1 digit, then the signal is weak, if 2 digits, then the signal is normal and if 3 digits or more .. it means the signal is strong.
Here you go!
package main
import (
"fmt"
)
func CountDigits(i int) (count int) {
for i != 0 {
i /= 10
count = count + 1
}
return count
}
func main() {
var i int
fmt.Println("Enter an integer value : ")
_, err := fmt.Scanf("%d", &i)
if err != nil {
fmt.Println(err)
}
fmt.Println("You have entered a : ", CountDigits(i), "digit(s) integer value")
}
See also : Golang : Number guessing game with user input verification example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.7k Golang : Fix go.exe is not compatible with the version of Windows you're running
+5k Golang : The Tao of importing package
+14.2k Golang : On enumeration
+8k Golang : Auto-generate reply email with text/template package
+6.1k Golang : Detect face in uploaded photo like GPlus
+8.8k Golang : Serving HTTP and Websocket from different ports in a program example
+16.3k Golang : File path independent of Operating System
+4.7k JQuery : Calling a function inside Jquery(document) block
+7.3k Golang : Detect sample rate, channels or latency with PortAudio
+31.9k Golang : Math pow(the power of x^y) example
+11.9k Golang : Split strings into command line arguments
+7.9k How to show different content from website server when AdBlock is detected?