Golang Program to Count the Number of Digits in a Number



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
Updated on: 2021-07-31T15:00:03+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements