Kotlin program to count digits in an integer number

Kotlin | Count digits in a number: Here, we are going to learn how to count the total number of digits of a given integer number in Kotlin programming language? Submitted by IncludeHelp, on April 22, 2020

Kotlin - Count digits in an integer number

Given an integer N, we have to count total number of digits.

Example

Input:
N = 123

Output:
Total digits: 3

Input:
N = 12345

Output:
Total digits: 5

Program to count digits in a number in Kotlin

package com.includehelp.basic

import java.util.*

// Main Function entry Point of Program
fun main(args: Array<String>) {
    // Input Stream
    val scanner = Scanner(System.`in`)
    
    // Input number
    println("Enter Number  : ")
    var number: Long = scanner.nextLong()
    
    var count=0
    // Count Number of Digits in numbers
    while (number>0){
        number/=10
        count++
    }
    
    // print count
    println("Number of Digits : $count")
}

Output

Run 1:
Enter Number  :
2343434544
Number of Digits : 10

-------
Run 2:
Enter Number  :
123456
Number of Digits : 6

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.