Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Go Program to Count Digits in a Number

by suresh

Write a Go Program to Count Digits in a Number using for loop. The for loop condition (num > 0) make sure the number is greater than zero. Within the loop, we are incrementing the count value. Next, we divide the number by ten (num = num / 10), which will remove the last digit from a number.

package main

import "fmt"

func main() {

    var num, count int
    count = 0

    fmt.Print("Enter any number to count digits = ")
    fmt.Scanln(&num)

    for num > 0 {
        num = num / 10
        count = count + 1
    }
    fmt.Println("The total Number to Digits = ", count)
}
Go Program to Count Digits in a Number 1

Golang Program to Count Digits in a Number

In this Golang example, we altered the for loop and counted the total number of individual digits in a user-given value.

package main

import "fmt"

func main() {

    var num, count int

    fmt.Print("Enter any number to count digits = ")
    fmt.Scanln(&num)

    for count = 0; num > 0; num = num / 10 {
        count = count + 1
    }
    fmt.Println("The total Number to Digits = ", count)
}
Go Program to Count Digits in a Number 2

Go example to Count Digits in a Number using a function

package main

import "fmt"

func digitCount(num int) int {
    var count int = 0
    for num > 0 {
        num = num / 10
        count = count + 1
    }
    return count
}

func main() {

    var num, count int

    fmt.Print("Enter any number to count digits = ")
    fmt.Scanln(&num)

    count = digitCount(num)
    fmt.Println("The total Number to Digits = ", count)
}
Go Program to Count Digits in a Number 3

In this Go Program to return Digits in a Number, we call digitCount(num / 10) function recursively with the updated value. 

package main

import "fmt"

var count int = 0

func digitCount(num int) int {
    if num > 0 {
        count = count + 1
        digitCount(num / 10)
    }
    return count
}

func main() {

    var num int

    fmt.Print("Enter any number to count digits = ")
    fmt.Scanln(&num)

    count = digitCount(num)
    fmt.Println("The total Number to Digits = ", count)
}
Golang Program to Count Digits in a Number 4

Placed Under: Go Examples

  • Golang Hello World Program
  • Go Add Two Number
  • Go Compound Interest
  • Go Count Digits in a Number
  • Go Cube of a Number
  • Go Even or Odd
  • Go Largest of Three Numbers
  • Go Reverse Natural Numbers
  • Go Multiplication Table
  • Go Number divisible by 5 and 11
  • Go Positive or Negative
  • Go Power of a Number
  • Go Print Natural Numbers
  • Go Profit or Loss
  • Go Print 1 to 100 without loop
  • Go Program to Print 1 to 100
  • Go Product of Digits in a Number
  • Go Palindrome Number Program
  • Go Print Even Numbers
  • Go Print Odd Numbers
  • Go Factors of a Number
  • Go Perfect Number
  • Go Prime Number
  • Go Reverse a Number
  • Go Simple Interest
  • Go Square of a Number
  • Go Square root of a Number
  • Go Sum of Digits in a Number
  • Go Sum & Avg of Natural Nums
  • Go Sum of Even Numbers
  • Go Sum of Odd Numbers
  • Go Sum of Even and Odd
  • Go Sphere Vol & Surface Area
  • Go Cylinder Vol & Surface Area
  • Go Cuboid Vol & Surface Area
  • Go Cube Volume & Surface Area
  • Go Cone Volume & Surface Area

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy