Kotlin Program to Calculate Simple Interest

In this article, we will understand how to calculate the Simple Interest in Kotlin. It is the percentage interest on total principal amount. Returns are less compared to Compound Interest. Simple Interest is calculated using the formula

(principle*rate*time)/100

Below is a demonstration of the same

Suppose our input is

Principle number: 100000
Interest rate: 5
Time period in years: 2

The desired output would be

Simple Interest: 1000

Algorithm

  • Step 1 ? START

  • Step 2 ? Declare four integer values principalAmount, interestRate, timePeriod, simpleInterest

  • Step 3 ? Define the values for principalAmount, interestRate, timePeriod, simpleInterest

  • Step 4 ? Perform "(principle*rate*time)/100" to calculate the simple interest and store it in a simpleInterest variable

  • Step 5 ? Display simpleInterest

  • Step 6 ? STOP

Example 1

In this example, we will calculate Simple Interest in Kotlin using the above given formulae. First, declare and set the variable for the Principal amount

val principalAmount = 10000

Then, set the variables for rate of interest and time period ?

val interestRate = 5
val timePeriod = 3

Now, use the above formulae to calculate the Simple Interest with the Principal, Rate of Interest and Time Period ?

val simpleInterest = (principalAmount*interestRate*timePeriod)/100

Let us now see the example to compute the Simple Interest 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> principalAmount <span class="token operator">=</span> <span class="token number">10000</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Principal amount is defined as: <span class="token interpolation variable">$principalAmount</span>"</span><span class="token punctuation">)</span>
   
   <span class="token keyword">val</span> interestRate <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The rate of interest is defined as: <span class="token interpolation variable">$interestRate</span> %"</span><span class="token punctuation">)</span>
   
   <span class="token keyword">val</span> timePeriod <span class="token operator">=</span> <span class="token number">2</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The time period is defined as: <span class="token interpolation variable">$timePeriod</span> years"</span><span class="token punctuation">)</span>
   
   <span class="token keyword">val</span> simpleInterest <span class="token operator">=</span> <span class="token punctuation">(</span>principalAmount<span class="token operator">*</span>interestRate<span class="token operator">*</span>timePeriod<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">100</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"<br> Simple Interest is: <span class="token interpolation variable">$simpleInterest</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

Principal amount is defined as: 10000
The rate of interest is defined as: 5 %
The time period is defined as: 2 years

Simple Interest is: 1000

Example 2

In this example, we will calculate Simple Interest 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> principalAmount <span class="token operator">=</span> <span class="token number">10000</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Principal amount is defined as: <span class="token interpolation variable">$principalAmount</span>"</span><span class="token punctuation">)</span>

   <span class="token keyword">val</span> interestRate <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The rate of interest is defined as: <span class="token interpolation variable">$interestRate</span> %"</span><span class="token punctuation">)</span>

   <span class="token keyword">val</span> timePeriod <span class="token operator">=</span> <span class="token number">2</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The time period is defined as: <span class="token interpolation variable">$timePeriod</span> years"</span><span class="token punctuation">)</span>
   <span class="token function">getSimpleInterest</span><span class="token punctuation">(</span>principalAmount<span class="token punctuation">,</span> interestRate<span class="token punctuation">,</span> timePeriod<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">fun</span> <span class="token function">getSimpleInterest</span><span class="token punctuation">(</span>principalAmount<span class="token operator">:</span> Int<span class="token punctuation">,</span> interestRate<span class="token operator">:</span> Int<span class="token punctuation">,</span> timePeriod<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> simpleInterest <span class="token operator">=</span> <span class="token punctuation">(</span>principalAmount<span class="token operator">*</span>interestRate<span class="token operator">*</span>timePeriod<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">100</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"<br> Simple Interest is: <span class="token interpolation variable">$simpleInterest</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

Principal amount is defined as: 10000
The rate of interest is defined as: 5 %
The time period is defined as: 2 years

Simple Interest is: 1000
Updated on: 2022-10-13T12:53:38+05:30

534 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements