Q:

Kotlin program to count digits in an integer number

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now