{

How to count the number of digits in an integer| Go by Examples


Golang Number of digits examples

This example covers two programs to count the number of digits in an integer.

  • the first program uses the iterative function,
  • The second program uses the Recursive function.

Consequently, Both are used to implement digit count for a given number.

you should be familiar with the following Go language features for this program.

The below code writes to read an integer value from a user keyboard and find the number of digits.

For example, if a user inputs 245 as a number, the result of this program is 3.

How to get the count digit in an integer using an iterative function?

Here is a step-by-step.

  • The program reads an integer from the user console and stores it in a variable number.
  • The For Loop is used to iterate through all the digits until the condition (number!= 0) is false, i.e. number=0.

For Input number=456, the Iteration function executes the sequence of steps.

  • Once the First Iteration of for loop is complete, the number will be 45 and the count is incremented by 1, count=1.
  • After the second iteration is completed, the number will be 4, and the count is incremented by 1 ie count=2.
  • After the third iteration is completed, a number will be 0, and the count is incremented by 1 ie count=3.
  • For loop condition expression is evaluated to false(number=0), For loop is terminated.
  • the count is returned to the main function

Finally, a count is printed to the console using the Println function in the main function.

package main

import "fmt"

func iterativeDigitsCount(number int) int {
    count: = 0
    for number != 0 {
        number /= 10
        count += 1
    }
    return count

}
func main() {
    number: = 0
    fmt.Print("Please enter number: ")
    fmt.Scanln( & number)
    fmt.Printf("Digits count of %d is %d", number, iterativeDigitsCount(number))

}

When you run the above program, the Output is

Please enter number: 456  
Digits count of 456 is 3  
Please enter the number: 2  
A digits count of 2 is 1  

How to count the number of digits of an integer using the Recursive function

A recursive function is a function that calls inside the same function.

Here is a recursive Function execution flow for the given input number=2546.

recursionCountDigits(2546)  
\-- 1+ recursionCountDigits(254)  
   -- 2+ recursionCountDigits(25)  
      -- 3+ recursionCountDigits(2)  
         -- 3+ 1 returns 4 to the main function  

Following is an example of a Recursive function to count the number of digits of a number

package main

import "fmt"


func main() {
    number: = 0
    fmt.Print("Please enter number: ")
    fmt.Scanln( & number)
    fmt.Printf("Digits count of %d is %d", number, recursionCountDigits(number))

}

func recursionCountDigits(number int) int {
    if number < 10 {
        return 1
    } else {
        return 1 + recursionCountDigits(number / 10)
    }
}

Output is

Please enter number: 2546  
Digits count of 2546 is 4  
  
Please enter number: 456789  
Digits count of 456789 is 6  

Conclusion

Learn how to calculate the digit count in a given integer number in the golang program using iterative and recursive functions.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.