ADVERTISEMENT
ADVERTISEMENT

Home » Kotlin » Kotlin programs » Kotlin basic programs

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

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.