Swift Program to Calculate Simple Interest

This tutorial will discuss how to write a swift program to calculate simple interest.

Simple interest is the most commonly used method to find the interest on the money for a given time period. In this method, the interest is always applied to the original principal amount with same the interest rate for every time period. It is calculated by multiplying the interest rate by the principal amount and the time.

Formula

Following is the formula of simple interest ?

S.I = Principal Amount(P) x Time(T) x Rate(R)/100

Where

Principal(P) ? Principal amount represents the amount that is initially invested.

Rate(R) ? Rate represents the interest rate which applied to the principal amount for the given time period.

Time(T) ? Time represents the time period for which the amount is invested in years.

Algorithm to Calculate Simple Interest

  • Step 1 ? Define three variables (Principal, Rate and Time)

  • Step 2 ? Assign the value of those variables

  • Step 3 ? Implement Simple interest formula (Principal Amount(P) x Time(T) x Rate(R)/100)

  • Step 4 ? Print the output

Example

The following program shows how to calculate Simple Interest.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc
<span class="token keyword">var</span> principal <span class="token operator">=</span> <span class="token number">20000</span>
<span class="token keyword">var</span> rate <span class="token operator">=</span> <span class="token number">3</span>
<span class="token keyword">var</span> time <span class="token operator">=</span> <span class="token number">5</span>
<span class="token keyword">var</span> SimpleInterest <span class="token operator">=</span> <span class="token punctuation">(</span>principal <span class="token operator">*</span> time <span class="token operator">*</span> rate<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">100</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Principal Amount is - "</span><span class="token punctuation">,</span> principal<span class="token punctuation">,</span><span class="token string">"rupees"</span><span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Rate of the interest is - "</span><span class="token punctuation">,</span> rate<span class="token punctuation">,</span><span class="token string">"%"</span><span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Time period is -"</span><span class="token punctuation">,</span> time<span class="token punctuation">,</span> <span class="token string">"Years"</span><span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"\nFinal simple interest is - "</span><span class="token punctuation">,</span> SimpleInterest<span class="token punctuation">,</span><span class="token string">"rupees"</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 - 20000 rupees 
Rate of the interest is - 3 % 
Time period is - 5 Years
Final simple interest is - 3000 rupees

In the above code, we calculate simple interest using mathematical formula as shown in the below code ?

var SimpleInterest = (principal * time * rate)/100

Here, the principal, rate, and time period are 20000, 3, and 5 so the simple interest is 3000.

Example

The following program shows how to calculate Simple Interest with user-defined input.

<div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the principal amount"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> principal <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the interest rate"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> rate <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the time period"</span><span class="token punctuation">)</span>

<span class="token keyword">var</span> time <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>

<span class="token keyword">var</span> SimpleInterest <span class="token operator">=</span> <span class="token punctuation">(</span>principal <span class="token operator">*</span> time <span class="token operator">*</span> rate<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">100</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"\nEntered Principal Amount is - "</span><span class="token punctuation">,</span> principal<span class="token punctuation">,</span><span class="token string">"rupees"</span><span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered Interest Rate is - "</span><span class="token punctuation">,</span> rate<span class="token punctuation">,</span><span class="token string">"%"</span><span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered Time period is -"</span><span class="token punctuation">,</span> time<span class="token punctuation">,</span><span class="token string">"Years"</span><span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"\nFinal simple interest is - "</span><span class="token punctuation">,</span> SimpleInterest<span class="token punctuation">,</span><span class="token string">"rupees"</span><span class="token punctuation">)</span>
</div>

STDIN Input

Please enter the principal amount
10000 
Please enter the interest rate 
4 
Please enter the time period 
5

Output

Entered Principal Amount is -  10000 rupees
Entered Interest Rate is -  4 %
Entered Time period is - 5 Years

Final simple interest is -  2000 rupees

In the above code, we calculate simple interest using mathematical formula as shown in the below code ?

var SimpleInterest = (principal * time * rate)/100

Here the principal, rate, and time period are taken from the user at runtime using the readLine() function. So the simple interest of the given data is 3000.

Updated on: 2022-08-04T08:10:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements