How to Count the Number of Digits in an Integer in Golang?

Go ProgrammingServer Side ProgrammingProgramming

In this tutorial, we are going to find the number of digits in an integer using the Golang programming language. This tutorial will include two ways to find the number of digits in an integer, one by using the for loop and another by the recursion concept.

Explanation

Number = 5436

The number of the digit in the above number is 4.

Number = 783

The number of the digit in the above number is 3.

Logic Explanation

Suppose we have a number 32456 we are storing it into a tempNumber in the above code.

tempNumber = 32456

count = 0

Now we are running a for loop since it becomes zero.

  • Iteration

    • Increasing the count to 1
    • tempNumber = tempNumber / 10
    • = 32456 / 10
    • = 3245
  • Iteration

    • Increasing the count to 2
    • tempNumber = tempNumber / 10
    • = 3245 / 10
    • = 324
  • Iteration

    • Increasing the count to 3
    • tempNumber = tempNumber / 10
    • = 324 / 10
    • = 32
  • Iteration

    • Increasing the count to 4
    • tempNumber = tempNumber / 10
    • = 32 / 10
    • = 3
  • Iteration

    • Increasing the count to 5
    • tempNumber = tempNumber / 10
    • = 3 / 10
    • = 0

In this way, we are calculating the count of digits in an integer.

Using the for loop to find the number of digits in an integer.

Algorithm

  • Step 1 − Declaring the int type variable in which we will store the number whose number of digits we have to find.

  • Step 2 − Initialize the number with the respective value whose number of digits we have to find.

  • Step 3 − Run a for loop till the number becomes zero.

  • Step 4 − Print the result.

Example 1

In this example, we are going to use the for loop to find the number of digits in an integer.

package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the variable whose number of digits we have to find and a // count variable that will store the number of digits var number, count int // initializing the variables with the respective number number = 32456 count = 0 fmt.Println("Program to find the number of digits in an integer using the for loop.") // creating the temp variable as we have to perform some // arithmetic operations tempNumber := number // running a for loop to find the number of digits in an integer for tempNumber != 0 { count++ // dividing the number 10 and storing it into the same variable // that is eliminating the last digit tempNumber = tempNumber / 10 } // printing the result fmt.Println("The total number of digits in", number, "is", count) }

Output

Program to find the number of digits in an integer using the for loop.
The total number of digits in 32456 is 5

Using the recursion concept to find the number of digits in an integer.

Algorithm

  • Step 1 − Declaring the int type variable in which we will store the number whose number of digits we have to find.

  • Step 2 − Initialize the number with the respective value whose number of digits we have to find.

  • Step 3 − Call the recursive function.

  • Step 4 − Print the result.

Example 2

In this example, we are going to use the recursion concept to find the number of digits in an integer.

package main // fmt package provides the function to print anything import ( "fmt" ) // this is a function which have a parameter of int type and also // have a int return type func numberOfDigit(tempNumber int) int { if tempNumber == 0 { return 0 } // dividing the number by 10 and storing it into the same variable // that is eliminating the last digit tempNumber = tempNumber / 10 // calling the recursive function and return by increasing when // the function call stops return numberOfDigit(tempNumber) + 1 } func main() { // declaring the variable whose number of digits we have to find and a // count variable that will store the number of digits var number, count int // initializing the variables with the respective number number = 7865 count = 0 fmt.Println("Program to find the number of digits in an integer using the recursive function.") // creating the temp variable as we have to perform some // arithmetic operations tempNumber := number // calling the recursive function to find the number of digits in an integer count = numberOfDigit(tempNumber) // printing the result fmt.Println("The total number of digits in", number, "is", count) }

Output

Program to find the number of digits in an integer using the recursive function.
The total number of digits in 7865 is 4

Conclusion

These are the two ways to find the number of digits in an integer in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about Golang you can explore these tutorials.

raja
Updated on 30-Sep-2022 07:58:46

Advertisements