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.

-- function declaration for function simpleInterest simpleInterest :: Float->Float->Float->Float -- function definition for function simpleInterest simpleInterest p t r = (p*t*r)/100 main = do -- declaring and initializing variables for interest parameters let p = 1000 let t = 2 let r = 3 -- invoking the function simpleInterest and loading into a variable interest let interest = simpleInterest p t r -- printing the interest print("Principle Amount, Time and Rate of Interest are =") print(p,t,r) print ("The Simple interest is:") print (interest)

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.

-- function declaration for function compoundInterest compoundInterest :: Float->Float->Float->Float->Float -- function definition for function compoundInterest compoundInterest p t r n = p*((1 + r/(100*n))**(n*t)) - p main = do -- declaring and initializing variables for interest parameters let p = 1000 let t = 2 let r = 3 let n = 1 -- invoking the function simpleInterest and loading into a variable interest let interest = compoundInterest p t r n -- printing the interest print("Principle Amount, Time and Rate of Interest are =") print(p,t,r) print ("The Compound interest is:") print (interest)

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.

Updated on: 24-Nov-2022

267 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements