Golang Program to Count the Number of Digits in a Number

Go ProgrammingServer Side ProgrammingProgramming

Suppose the number is: 123456

Count of digits in the given number is: 6

To count the number of digits in a number, we can take following

Steps

  • Take the value of the integer and store in a variable.
  • Using a while loop, get each digit of the number and increment the count each time a digit is obtained.
  • Print the number of digits in the given integer.

Example

 Live Demo

package main
import "fmt"
func main(){
   var n int
   fmt.Print("Enter the number: ")
   fmt.Scanf("%d", &n)
   count := 0
   for n >0 {
      n = n/10
      count++
   }
   fmt.Printf("The number of digits in the given number is: %d", count)
}

Output

Enter the number: 123456
The number of digits in the given number is: 6
raja
Published on 31-Jul-2021 14:58:36
Advertisements