How to Calculate Compound Interest using Golang code?

In this tutorial, we are going to see the program for calculating compound interest in Golang. It is a method of finding the accurate interest on loans in the banking and finance sector using factors like principal amount, rate per annum, and time. Due to more accuracy, the rates are higher than the simple interest.

Formula

compound_Interest = P * ( 1 + R / 100) ^ T
P = Principal amount
R = Rate per annum
T = Time

For example, suppose the principal amount is 1000, the rate of interest is 4 and the interval is 2 years then the compound interest is.

compound_interest = 1000 * (1 + 4 / 100 ) ^2
= 1081.6000000000001

Finding the compound interest within the function

Algorithm

  • STEP 1 ? Declaring the variables for the principal amount, rate of interest, time, and the compound interest of float64 data type.

  • STEP 2 ? Taking the input for the principal amount, rate of interest, and time from the user.

  • STEP 3 ? Finding the compound interest using the above formula within the function.

  • STEP 4 ? Printing the result.

Time Complexity

O(1) - The time complexity is constant because no matter what is the input the program will take sane time.

Space Complexity

O(1) - The variables are static in the program so the space complexity is also constant

Example 1

In this example, we will find the compound interest within the function.

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"math"</span>
<span class="token punctuation">)</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// declaring the floating variables using the var keyword</span>
   <span class="token comment">// for storing the principal, rate of interest, and time</span>
   <span class="token keyword">var</span> principal<span class="token punctuation">,</span> rateOfInterest<span class="token punctuation">,</span> time<span class="token punctuation">,</span> compoundInterest float64
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find compound interest."</span><span class="token punctuation">)</span>
   
   <span class="token comment">// initializing the principal</span>
   principal <span class="token operator">=</span> <span class="token number">3000</span>
   
   <span class="token comment">// initializing the rate</span>
   rateOfInterest <span class="token operator">=</span> <span class="token number">4</span>
   
   <span class="token comment">// initializing the</span>
   time <span class="token operator">=</span> <span class="token number">3</span>
   
   <span class="token comment">// finding the compound interest</span>
   compoundInterest <span class="token operator">=</span> <span class="token punctuation">(</span>principal <span class="token operator">*</span> <span class="token class-name"><span class="token namespace">math<span class="token punctuation">.</span></span>Pow</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">+</span>rateOfInterest<span class="token operator">/</span><span class="token number">100</span><span class="token punctuation">,</span> time<span class="token punctuation">)</span><span class="token punctuation">)</span>
   
   <span class="token comment">// printing the result</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Principal ="</span><span class="token punctuation">,</span> principal<span class="token punctuation">,</span> <span class="token string">"\nRate of Interest"</span><span class="token punctuation">,</span> rateOfInterest<span class="token punctuation">,</span> <span class="token string">"\nTime"</span><span class="token punctuation">,</span> time<span class="token punctuation">,</span> <span class="token string">"\nThe compound interest="</span><span class="token punctuation">,</span> compoundInterest<span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"(Finding the compound interest within the function)"</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

Program to find compound interest.
Principal = 3000
Rate of Interest 4
Time 3
The compound interest= 3374.592
(Finding the compound interest within the function) 

Description of code

  • var principal, rateOfInterest, time, compoundInterest float64 ? In this line we are declaring the principal, rate of interest, time, and compound interest that we are going to use later. As the interest can be in decimal so we have used the float data type.

  • fmt.Scanln(&principal) ? Taking the input for principal from the user.

  • fmt.Scanln(&rateOfInterest) ? Taking the input for the rate of interest from the user

  • fmt.Scanln(&time) ? Taking the input for the time from the user.

  • compoundInterest = (principal * math.Pow(1+rateOfInterest/100, time)) ? In this line of code, we are applying the formula and finding the compound interest. Also, to find the 1+rateOfInterest/100 power of the time we have used the math library in Golang which has the Pow() function which has two parameters of float type.

  • fmt.Println("The compound interest is", compoundInterest) ? In later printing the result.

Finding the compound interest in different function

Algorithm

  • STEP 1 ? Declaring the variables for the principal amount, rate of interest, time, and the compound interest of float64 data type.

  • STEP 2 ? Taking the input for the principal amount, rate of interest, and time from the user.

  • STEP 3 ? Calling the function with the principal amount, rate of interest, and time as a parameter, and storing the compound interest the function is returning.

  • STEP 4 ? Printing the result.

Example 2

In this example, we will find the compound interest in the separate function and call in the function where we want to print.

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

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"math"</span>
<span class="token punctuation">)</span>
func <span class="token function">compoundInterestFunction</span><span class="token punctuation">(</span>principal<span class="token punctuation">,</span> rateOfInterest<span class="token punctuation">,</span> time float64<span class="token punctuation">)</span> float64 <span class="token punctuation">{</span>

   <span class="token comment">// finding the compound interest</span>
   compoundInterest <span class="token operator">:</span><span class="token operator">=</span> <span class="token punctuation">(</span>principal <span class="token operator">*</span> <span class="token class-name"><span class="token namespace">math<span class="token punctuation">.</span></span>Pow</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">+</span>rateOfInterest<span class="token operator">/</span><span class="token number">100</span><span class="token punctuation">,</span> time<span class="token punctuation">)</span><span class="token punctuation">)</span>
   <span class="token comment">// returning the compound interest</span>
   <span class="token keyword">return</span> compoundInterest
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// declaring the floating variables using the var keyword</span>
   <span class="token comment">// for storing the principal, rate of interest, and time</span>
   <span class="token keyword">var</span> principal<span class="token punctuation">,</span> rateOfInterest<span class="token punctuation">,</span> time<span class="token punctuation">,</span> compoundInterest float64
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find compound interest."</span><span class="token punctuation">)</span>
   
   <span class="token comment">// taking the principal as input from the user</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token string">"Please enter the value of the principal amount = "</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>principal<span class="token punctuation">)</span>
   
   <span class="token comment">// taking the rate of interest as input from the user</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token string">"Please enter the value of the rate of interest = "</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>rateOfInterest<span class="token punctuation">)</span>
   
   <span class="token comment">// taking the value of the time as input from the user</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token string">"Please enter the value of the time = "</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>time<span class="token punctuation">)</span>
   
   <span class="token comment">// calling the compound interest function by passing the respective parameter</span>
   <span class="token comment">// and storing the result</span>
   compoundInterest <span class="token operator">=</span> <span class="token function">compoundInterestFunction</span><span class="token punctuation">(</span>principal<span class="token punctuation">,</span> rateOfInterest<span class="token punctuation">,</span> time<span class="token punctuation">)</span>
   
   <span class="token comment">// printing the result</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The compound interest is"</span><span class="token punctuation">,</span> compoundInterest<span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"(Finding the compound interest in different function)"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div>

Output

Program to find compound interest.
Please enter the value of the principal amount = 1000
Please enter the value of the rate of interest = 5
Please enter the value of the time = 3
The compound interest is 1157.6250000000002
(Finding the compound interest in different function)

Description of code:

  • var principal, rateOfInterest, time, compoundInterest float64 ? In this line we are declaring the principal, rate of interest, time, and compound interest that we will use later. As the interest can be in decimal so we have used the float data type.

  • fmt.Scanln(&principal) ? Taking the input for principal from the user.

  • fmt.Scanln(&rateOfInterest) ? Taking the input for the rate of interest from the user.

  • fmt.Scanln(&time) ? Taking the input for the time from the user.

  • compoundInterest = compoundInterestFunction(principal, rateOfInterest, time)

    ? In this line of code we are calling the function that is finding the compound interest by passing the respective parameters. Also, to find the 1+rateOfInterest/100 power of the time we have used the math library in Golang which has the Pow() function which has two parameters of float type.

  • fmt.Println("The compound interest is", compoundInterest) ? In later printing the result.

Conclusion

These are the two ways to find compound interest in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about go you can explore these tutorials.

Updated on: 2022-08-29T07:37:29+05:30

653 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements