Kotlin Program to Print an Integer

In this article, we will understand how to print an integer. Integer is a primitive data type that contains numbers up to 32 bits.

Below is a demonstration of the same ?

Suppose our input is ?

45

The desired output would be ?

The integer is: 45

Algorithm

  • Step 1 ? START

  • Step 2 ? Define the integer value in a variable

  • Step 3 ? Display it on the console

  • Step 4 ? STOP

Example 1

In this example, we will simply print an integer using the print() method. First, declare an integer variable, which will be our input number

val inputInt = 45

Now, print the above integer using the print() method ?

print("The integer is defined as: " +inputInt)

Let us now see the complete example ?

<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">val</span> inputInt <span class="token operator">=</span> <span class="token number">45</span>
   <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"The integer is defined as: "</span> <span class="token operator">+</span>inputInt<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 integer is defined as: 45

Example 2

Here, we have created a custom function and printed 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">printInt</span><span class="token punctuation">(</span>inputInt <span class="token operator">:</span> Int<span class="token punctuation">)</span><span class="token punctuation">{</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The integer is defined as: "</span> <span class="token operator">+</span>inputInt<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<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">val</span> inputInt <span class="token operator">=</span> <span class="token number">45</span>
   <span class="token function">printInt</span><span class="token punctuation">(</span>inputInt<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 integer is defined as: 45
Updated on: 2022-10-13T12:19:29+05:30

760 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements