Kotlin Program to Count Digits of Given Number

In this post, we will go through Kotlin program to count number of digits of a number.

We can check this using below ways –

  1. Count Digits using Loop
  2. Count Digit using Recursion
  3. Count Digit using Logarithmic Formula

1. Count Digits Using Loop

Sourcecode –

import java.util.*

fun main() {

    val scanner = Scanner(System.`in`)

    println("Enter number")
    val n = scanner.nextInt()

    var curNum = n
    var count = 0
    while (curNum != 0) {
        curNum /= 10
        count++
    }

    println("Digit Count: $count")
}

When you run the program, output will be

Enter number
6540896
Digit Count: 7

Suppose we have entered 3223.
So, it will be stored in n.
Then, n will stored in curNum. This is because we can not change n (defined using val).

Initially,

curNum = 3223, count = 0

At first iteration,

(3223 != 0) is true. So,
curNum = 3223 / 10 => curNum = 322
count++ => count = count + 1 => count = 0 + 1 => count = 1

After first iteration,
curNum = 322, count = 1

At Second iteration,

(322 != 0) is true. So,
curNum = 322 / 10 => curNum = 32
count++ => count = 1 + 1 => count = 2

After second iteration,
curNum = 32, count = 2

At Third iteration,

(32 != 0) is true. So,
curNum = 32 / 10 => curNum = 3
count++ => count = 2 + 1 => count = 3

After third iteration,
curNum = 3, count = 3

At Fourth iteration,

(3 != 0) is true. So,
curNum = 3 / 10 => curNum = 0
count++ => count = 3 + 1 => count = 4

After Fourth iteration,
curNum = 0, count = 4

At fifth iteration,

At Second iteration,

(0 != 0) is false. So,

While loop breaks.

Finally, number is digits is value stored in count. So, count is printed using println().

Now, we see how to do above process using recursion.

2. Count Digits Using Recursion

import java.util.*

fun main() {

    val scanner = Scanner(System.`in`)

    println("Enter number")
    val n = scanner.nextInt()

    val count = countDigit(n)

    println("Digit Count: $count")
}

private fun countDigit(n: Int): Int {
    if(n == 0) return 0
    return 1 + countDigit(n / 10)
}

When you run the program, output will be

Enter number
867588587
Digit Count: 9

This program works same as explained. It’s just that we are using recursion here.

Now, we use logarithmic formula to find number of digits in a number.

3. Count Digits Using Log Formula

We use formula

Digit Count of n = Upper Bound of log10(n)
Where, n is positive number

to find count of digits in a number.

Sourcecode –

import java.util.*
import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.roundToInt

fun main() {

    val scanner = Scanner(System.`in`)

    println("Enter number")
    val n = scanner.nextDouble()

    val count: Int = floor(log10(n) + 1).roundToInt()

    println("Digit Count: $count")
}

When you run the program, output will be

Enter number
645654365
Digit Count: 9

When you run the program, you are asked to enter a number.
Entered number is read using scanner.nextDouble() and stored in variable n.
Note: You can use nextInt() as well.

After that, we are using logarithmic formula to find number of digits present in n.
We already know that we are using decimal system here. It means it has some relation with power of 10.
So, we can use log10(logarithm of base 10) to count number of digits in n.

Digit Count of n = Upper Bound of log10(n)
Where, n is positive number

Note: Logarithm does not work with negative number.