Hello World program in Kotlin

In this article, we will understand how to print hello world in Kotlin. Hello world is stored as a string format and is printed using ?print()? function.

Input

Suppose our input is

Hello World

Output

The expected out should be

Hello World

Algorithm

  • Step 1 ? START

  • Step 2 ? Define a string variable

  • Step 3 ? Display the string on the console

  • Step 4 ? STOP

Example 1

In this example, we will simply display a string "Hello World" using the print() method in Kotlin ?

<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> inputStr <span class="token operator">=</span> <span class="token string">"Hello World!"</span>

   <span class="token function">print</span><span class="token punctuation">(</span>inputStr<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

Hello World!

Example 2

Here, we have created a custom function and printed Hello World

<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">printStr</span><span class="token punctuation">(</span>inputStr <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">{</span>
   <span class="token function">print</span><span class="token punctuation">(</span>inputStr<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> inputStr <span class="token operator">=</span> <span class="token string">"Hello World!"</span>
   <span class="token function">printStr</span><span class="token punctuation">(</span>inputStr<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

Hello World!
Updated on: 2022-10-13T12:15:46+05:30

583 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements