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
+8.4k Golang : Format strings to SEO friendly URL example
+50.5k Golang : How to get time in milliseconds?
+6.4k Golang : Lock executable to a specific machine with unique hash of the machine
+7.6k Linux/Unix : fatal: the Postfix mail system is already running
+9.7k Golang : Simple image viewer with Go-GTK
+7.9k Golang : automatically figure out array length(size) with three dots
+7k Golang : Multiplexer with net/http and map
+20.2k SSL : How to check if current certificate is sha1 or sha2
+21k Golang : Repeat a character by multiple of x factor
+4.2k Python : Create Whois client or function example
+3.9k Which content-type(MIME type) to use for JSON data
+4.2k Unix/Linux : secure copying between servers with SCP command examples