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
+5.7k Golang : Generate multiplication table from an integer example
+4.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+5.2k Golang : fmt.Println prints out empty data from struct
+10.9k Golang : How to pipe input data to executing child process?
+13.4k Golang : Tutorial on loading GOB and PEM files
+19.1k Golang : How to count the number of repeated characters in a string?
+6.1k Golang : Selection sort example
+11.4k Golang : Calculations using complex numbers example
+9.7k CodeIgniter : Load different view for mobile devices
+7.6k Swift : Convert (cast) String to Double
+9.1k Golang : Find the length of big.Int variable example
+6.1k Unix/Linux : Use netstat to find out IP addresses served by your website server