Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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
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
Advertisements
