Kotlin Program to Count Number of Digits in an Integer

In this article, we will understand how to count the number of digits in an integer. The digits in an integer are counted using a loop and a counter.

Below is a demonstration of the same ?

Suppose our input is ?

Number : 15161718

The desired output would be ?

The result is : 8

Algorithm

  • Step 1 ? START

  • Step 2 ? Declare two integer values namely count and myInput.

  • Step 3 ? Define the values

  • Step 4 ? Using a while loop, divide the input value by 10 until the number is reduced to its lowest possible value. Increment the counter value each time.

  • Step 5 ? Display the counter value as result

  • Step 6 ? Stop

Example 1

In this example, we will count the number of digits in an Integer. First, declare and initialize two variables, one for count and another our input i.e., the input for which we want to count the number of digits ?

var count = 0
var myInput = 15161718

Then, count the number of digits in the input using a while loop ?

while (myInput != 0) {
   myInput /= 10
   ++count
}

Let us now see the complete example to count the number of digits in an integer ?

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">0</span>
   <span class="token keyword">var</span> myInput <span class="token operator">=</span> <span class="token number">15161718</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The number is defined as <span class="token interpolation variable">$myInput</span>"</span><span class="token punctuation">)</span>

   <span class="token keyword">while</span> <span class="token punctuation">(</span>myInput <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      myInput <span class="token operator">/=</span> <span class="token number">10</span>
      <span class="token operator">++</span>count
   <span class="token punctuation">}</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Number of digits: <span class="token interpolation variable">$count</span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

The number is defined as 15161718
Number of digits: 8

Example 2

In this example, we will count the Number of Digits in an Integer ?

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">var</span> myInput <span class="token operator">=</span> <span class="token number">15161718</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The number is defined as <span class="token interpolation variable">$myInput</span>"</span><span class="token punctuation">)</span>
   <span class="token function">countDigits</span><span class="token punctuation">(</span>myInput<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">fun</span> <span class="token function">countDigits</span><span class="token punctuation">(</span>input<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">var</span> myInput <span class="token operator">=</span> input
   <span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">0</span>
   <span class="token keyword">while</span> <span class="token punctuation">(</span>myInput <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      myInput <span class="token operator">/=</span> <span class="token number">10</span>
      <span class="token operator">++</span>count
   <span class="token punctuation">}</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Number of digits: <span class="token interpolation variable">$count</span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

The number is defined as 15161718
Number of digits: 8
Updated on: 2022-10-17T08:18:59+05:30

842 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements