Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Haskell Program to Calculate simple interest and compound interest
In this tutorial, we discuss writing a program to calculate simple and compound interest in the Haskell programming language
In this tutorial, we see
Program to compute Simple interest.
Program to compute Compound interest.
Simple interest is an Interest method for an investment, where interest is defined as I = p*t*r/100,
where p is the invested amount, t is the quantity of time in years, and r is the interest rate. (per 100).
Example ? For an invested amount of 1000(p), t= 2 and r =3 the interest is 60.
Compound interest is an Interest method for an investment, where interest is defined as I = p(1+r/n)^nt - p,
where p is the invented amount, t is the quantity of time in years, t is the interest rate for 100, and n is the number of times the money is compounded annually.
Example ? For an invested amount of 1000(p), t= 1, n=1, and r =3 the interest is 1030.
Algorithm steps
Declare or take input the amount and all the parameters.
Implement the program to calculate the simple and compound interest.
Print or Display the simple and compound interest.
Example 1
Program to compute Simple interest.
<div class="execute"></div><div class="code-mirror language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">-- function declaration for function simpleInterest</span> <span class="token hvariable">simpleInterest</span> <span class="token operator">::</span> <span class="token constant">Float</span><span class="token operator">-></span><span class="token constant">Float</span><span class="token operator">-></span><span class="token constant">Float</span><span class="token operator">-></span><span class="token constant">Float</span> <span class="token comment">-- function definition for function simpleInterest</span> <span class="token hvariable">simpleInterest</span> <span class="token hvariable">p</span> <span class="token hvariable">t</span> <span class="token hvariable">r</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token hvariable">p</span><span class="token operator">*</span><span class="token hvariable">t</span><span class="token operator">*</span><span class="token hvariable">r</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">100</span> <span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span> <span class="token comment">-- declaring and initializing variables for interest parameters</span> <span class="token keyword">let</span> <span class="token hvariable">p</span> <span class="token operator">=</span> <span class="token number">1000</span> <span class="token keyword">let</span> <span class="token hvariable">t</span> <span class="token operator">=</span> <span class="token number">2</span> <span class="token keyword">let</span> <span class="token hvariable">r</span> <span class="token operator">=</span> <span class="token number">3</span> <span class="token comment">-- invoking the function simpleInterest and loading into a variable interest</span> <span class="token keyword">let</span> <span class="token hvariable">interest</span> <span class="token operator">=</span> <span class="token hvariable">simpleInterest</span> <span class="token hvariable">p</span> <span class="token hvariable">t</span> <span class="token hvariable">r</span> <span class="token comment">-- printing the interest</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"Principle Amount, Time and Rate of Interest are ="</span><span class="token punctuation">)</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token hvariable">p</span><span class="token punctuation">,</span><span class="token hvariable">t</span><span class="token punctuation">,</span><span class="token hvariable">r</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The Simple interest is:"</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token hvariable">interest</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
"Principle Amount, Time and Rate of Interest are =" (1000.0,2.0,3.0) "The Simple interest is:" 60.0
In the above program, we declared a function simpleInterest which takes three floats as arguments and returns a float. In the function definition, we are taking three variables p,t, and r as arguments. We are computing the simple interest with appropriate computational logic and returning the computed value. In the main function, we declared and initialized three variables for principle amount(p), time(t), and interest rate(r). We finally invoked the function simpleInterest with arguments p,t, and r, loaded the returned value into a variable interest, and printed the interest.
Example 2
Program to compute Compound interest.
<div class="execute"></div><div class="code-mirror language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">-- function declaration for function compoundInterest</span> <span class="token hvariable">compoundInterest</span> <span class="token operator">::</span> <span class="token constant">Float</span><span class="token operator">-></span><span class="token constant">Float</span><span class="token operator">-></span><span class="token constant">Float</span><span class="token operator">-></span><span class="token constant">Float</span><span class="token operator">-></span><span class="token constant">Float</span> <span class="token comment">-- function definition for function compoundInterest</span> <span class="token hvariable">compoundInterest</span> <span class="token hvariable">p</span> <span class="token hvariable">t</span> <span class="token hvariable">r</span> <span class="token hvariable">n</span> <span class="token operator">=</span> <span class="token hvariable">p</span><span class="token operator">*</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">+</span> <span class="token hvariable">r</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token operator">*</span><span class="token hvariable">n</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token hvariable">n</span><span class="token operator">*</span><span class="token hvariable">t</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token hvariable">p</span> <span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span> <span class="token comment">-- declaring and initializing variables for interest parameters</span> <span class="token keyword">let</span> <span class="token hvariable">p</span> <span class="token operator">=</span> <span class="token number">1000</span> <span class="token keyword">let</span> <span class="token hvariable">t</span> <span class="token operator">=</span> <span class="token number">2</span> <span class="token keyword">let</span> <span class="token hvariable">r</span> <span class="token operator">=</span> <span class="token number">3</span> <span class="token keyword">let</span> <span class="token hvariable">n</span> <span class="token operator">=</span> <span class="token number">1</span> <span class="token comment">-- invoking the function simpleInterest and loading into a variable interest</span> <span class="token keyword">let</span> <span class="token hvariable">interest</span> <span class="token operator">=</span> <span class="token hvariable">compoundInterest</span> <span class="token hvariable">p</span> <span class="token hvariable">t</span> <span class="token hvariable">r</span> <span class="token hvariable">n</span> <span class="token comment">-- printing the interest</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"Principle Amount, Time and Rate of Interest are ="</span><span class="token punctuation">)</span> <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token hvariable">p</span><span class="token punctuation">,</span><span class="token hvariable">t</span><span class="token punctuation">,</span><span class="token hvariable">r</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The Compound interest is:"</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token hvariable">interest</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
"Principle Amount, Time and Rate of Interest are =" (1000.0,2.0,3.0) "The Compound interest is:" 60.900024
In the above program, we declared a function compoundInterest which takes four floats as arguments and returns a float. In the function definition, we are taking four variables p,t,n, and r as arguments. We are computing the compound interest with appropriate computational logic and returning the computed value. In the main function, we declared and initialized four variables for principle amount(p), time(t), annual compound count(n), and interest rate(r). We finally invoked the function compoundInterest with arguments p,t,n, and r, loaded the returned value into a variable interest, and printed the interest.
Conclusion
In this tutorial, we discussed implementing programs to calculate simple and compound interest in Haskell Programming Language.
