Kotlin Program to Reverse a Number

In this article, we will understand how to print an integer in Kotlin. The reverse of a number is computed using a loop and arithmetic operator % and /.

Below is a demonstration of the same ?

Suppose our input is ?

The number : 123456

The desired output would be ?

The result is 654321

Algorithm

  • Step 1 ? START

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

  • Step 3 ? Define the values

  • Step 4 ? Run a while loop

  • Step 5 ? Use modulus of 10 and get remainder for ?myTemp? .

  • Step 6 ? Multiply ?reversed? with 10, and add to ?myTemp?, and make that the current ?reversed?.

  • Step 7 ? Divide ?myInput? by 10, and make that the current ?myInput'.

  • Step 8 ? Display the result

  • Step 9 ? Stop

Example 1

In this example, we will reverse a number using a while loop. First, let us declare a variable for the input i.e. the number we will reverse. We have also declared a variable for the output i.e. reversed number  ?

var myInput = 123456
var reversed = 0

Now, using a while loop reverse the number. Loop till the input != 0;

while (myInput != 0) {
   val myTemp = myInput % 10
   reversed = reversed * 10 + myTemp
   myInput /= 10
}

Let us now see the complete example to reverse a number using a while loop ?

<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">123456</span>
   <span class="token keyword">var</span> reversed <span class="token operator">=</span> <span class="token number">0</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>
      <span class="token keyword">val</span> myTemp <span class="token operator">=</span> myInput <span class="token operator">%</span> <span class="token number">10</span>
      reversed <span class="token operator">=</span> reversed <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> myTemp
      myInput <span class="token operator">/=</span> <span class="token number">10</span>
   <span class="token punctuation">}</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The reversed number is: <span class="token interpolation variable">$reversed</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: 123456
The reversed number is: 654321

Example 2

In this example, we will reverse a number.

<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">123456</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">reverseNumber</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">reverseNumber</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> reversed <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>
      <span class="token keyword">val</span> myTemp <span class="token operator">=</span> myInput <span class="token operator">%</span> <span class="token number">10</span>
      reversed <span class="token operator">=</span> reversed <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> myTemp
      myInput <span class="token operator">/=</span> <span class="token number">10</span>
   <span class="token punctuation">}</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The reversed number is: <span class="token interpolation variable">$reversed</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: 123456
The reversed number is: 654321
Updated on: 2022-10-17T08:25:46+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements