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
+15.2k Golang : Find location by IP address and display with Google Map
+14.7k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+15.5k Golang : Get checkbox or extract multipart form data value example
+11k Golang : Web routing/multiplex example
+5.3k Javascript : How to loop over and parse JSON data?
+8.7k Golang : HTTP Routing with Goji example
+12.5k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+12.1k Golang : Get remaining text such as id or filename after last segment in URL path
+32.8k Golang : How to check if a date is within certain range?
+4.9k Linux : How to set root password in Linux Mint
+10.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+22k Golang : How to run Golang application such as web server in the background or as daemon?