Go Program To Count Number of Digits in an Integer

In this post, we will know how to count number of digits in an integer using Go Programming language.

Hello coders, in this post we will know how to count number of digits in an integer using Go Programming language.

Go Program To Count Number of Digits in an Integer


We will be using for loop to count the number of digits in the integer. The for loop condition (num > 0) make sure that the number is greater than 0, then we will divide the integer with 10, which will remove the last digit of the integer and increment the value of count by 1. This process will continue until num = 0 or num < 0.

For example: The number of digits in the integer 2020 is 4.

So, without any delay, let's begin the tutorial.

Table Of Contents 👊

Go Program To Count Number of Digits in an Integer

// Go Program To Count Number of Digits in an Integer
package main
import "fmt"

func main(){
  var num, count int 
  count = 0
  
  // Asking for Input
  fmt.Print("Enter an integer: ")
  fmt.Scan(&num)
  
  for num > 0{
    num = num / 10
    count = count + 1
  }
  fmt.Println("The Number of Digits are", count)
}

Output

Enter an integer: 2018
The Number of Digits are 4

How Does This Program Work ?

  var num, count int 
  count = 0

In this program, we have declared two int data type variables named as num and count. The value of count is assigned to 0.

  // Asking for Input
  fmt.Print("Enter an integer: ")
  fmt.Scan(&num)

Then, the user is asked to enter the integer. This integer will get stored in num named variable.

  for num > 0{
    num = num / 10
    count = count + 1
  }

Now, logic part of the program comes into play. This program checks whether the num is greater than 0, if found true then for loop is executed.

The integer stored in num variable is divided by 10, which removes the last digit of the integer. And it increases the value of count by 1. This keep happening until num = 0.

For example, Suppose a user enters 12345.

  • After first iteration, num becomes 1234 and count becomes 1.
  • After second iteration, num becomes 123 and count becomes 2.
  • After third iteration, num becomes 12 and count becomes 3.
  • After fourth iteration, num becomes 1 and count becomes 4.
  • After fifth iteration, num becomes 0 and count becomes 5.

After that loop statement stops, since num = 0.

  fmt.Println("The Number of Digits are", count)

And finally , the number stored in count is printed to the screen using fmt.Println() function.

Some of the used terms in this program are as follows:- 

package main - packages are a collection of Go source files that reside in the same directory. The line package main specifies that this file belongs to the main package.

Every Executable Go Application must contain the main function.

fmt.Println() - The fmt.Println() function in Go language is used to display output.

fmt.Scan() - The fmt.PrintScan() function is used to take input from the user in Go Programming language.

// - Used for Commenting in Go  

Go Program To Count Number of Digits in an Integer Using Functions

// Go Program To Count Number of Digits in an Integer
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 count, num int
  
  // Asking for Input
  fmt.Print("Enter an Integer: ")
  fmt.Scan(&num)
  
  // Calling Out function
  count = digitCount(num)
  fmt.Println("The Number of Digits are", count)
}

Output

Enter an Integer: 246802
The Number of Digits are 6
func digitCount(num int) int {
  var count int = 0
  for num > 0{
    num = num / 10
    count = count + 1
  }
  return count
}

In this program, we have declared a user defined function named as digitCount which returns the digits count which is stored in count named variable. Then, this function is called out when needed.

Conclusion

I hope after reading this post, you clearly understood how to count number of digits in an integer using Go Programming language.

If you still have any doubt regarding the topic, let us know in the comment section.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment