- Previous
- Next
In this example program for interest calculations, we will compute three different types of interests given the input – the principal amount, rate and time period.
The program intend to demonstrate the use of functions with return values for intermediate level learners of C++ programming.
This program is written using DEV C++ compiler version 4.9.9.2 installed on a Windows 7 64-bit PC. You may use any other standard C++ compiler such as Turbo C++ and the program will still work.
Problem Definition
There are three types of interests that we want to calculate using this program. Each of the interest is a separate function in the program that returns a value.
- Simple Interest
- Compound Interest
- Effective Annual Rate (Compound Interest that compounded many times during the year).
The program requires three input given below.
- Principal amount
- Rate in Percent
- Time in years
- Period
Period is when the user choose to compute Effective Annual Rate. The period is the number of times the interest compounded in a year – Half Yearly, Quarterly, Monthly and Daily.
Simple Interest
The simple interest is computed using the following formula.
A = (P + SI) = (P + Prt) => P (1 + rt)
Where
A = Total Amount with Interest
SI = simple interest
P = principle amount
r = rate in decimals
t = time in years
For Example:
Suppose P = $1000, r = 10% and time = 5 years. Convert rate into decimal value.
R = 10/100 = 0.10
A = 1000(1 + (0.10 * 5) = 1000(1 + 0.50) = 10<s>00</s> * 150/1<s>00 </s>= <strong>$1500</strong>.
SI = A – P
SI = 1500 – 1000 = <strong>$500.</strong>
Compound Interest
The difference between simple interest and compound interest is that the principal amount for simple interest does not change in the given time period, but for compound interest, for each compounding period then there is a new principal amount. The compound interest is computed using the following formula
A = P (1 + r)^t
Where
A = Total amount at the end of the time.
P = Principal amount
r = rate in percentage, rate = r/100
T = time in years
CI = Total Compounded Interest, CI = A – P
For example
Suppose principal = $2000, rate = 20% and time = 3 years, then
A = 2000(1 + 0.2)<sup>3 </sup>= 2000(1.2)<sup>3</sup> = 2000 * 1.728 = <strong>$3456</strong>
CI = 3456 – 2000 = <strong>$1456</strong>
Periodic Compounding and Effective Annual Rate
All examples of compound interest were calculated using a fixed percentage of interest rates. But what will be the compound interest if the compounding is done many times during the year.
The interest rates advertised by the bank will be 10%, but after compounding the interest rate changes and it all depends on how many times we compounded.
This new annual rate is called the Effective Annual Rate and original advertised rate is called the Nominal Rate.
The new formula for computing compound interest when the compounding is done many times during a year is given below.
A = P (1 + (r/n))^nt
Where
A = Total Compounded amount with the principal
P = principal amount
CI = total compounded interest earned
r = interest rate (nominal)
n = number of times compounded in a year
t = number of years
The formula for finding the Effective Annual Rate is
EAR = (1 + (r/n))^n – 1
For example,
Suppose P = $1000, nominal rate r = 10% and number of times we compounded in a year (monthly) = 12 and years = 3
A = 1000(1 + (0.10/12))<sup> 12*3</sup>
= 1000(1 + 0.0083)<sup>36</sup>
= 1000 (1.0083)<sup>36</sup>
= <strong>$1348.18</strong>
CI = 1348.18 – 1000
<strong>= $348.18</strong>
EAR = (1 + (0.10/12))<sup> 12</sup> – 1]
= (1 + 0.0083)<sup>12</sup> – 1
= 0.1046
= <strong>10.46%</strong>
Program Code – Interest Calculation Program
#include <iostream.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
double simple_interest (double principal, double rate, double time);
double compound_interest (double principal, double rate, double time);
double compound_interest_ear (double principal, double rate, double time, double period);
int main ()
{
double amount, principal, rate, time, interest, R;
int choice;
double SI, CI, EAR, Amount, period;
amount = principal = rate = time = interest = 0.0;
while (choice! = -99)
{
cout << "\n\n\n\n\n";
cout << "\t\t\tEnter Principal amount:"; cin >> principal;
cout << "\t\t\tEnter Rate (In percentage):"; cin >> R;
rate = R/100;
cout << "\t\t\tEnter Time (In Years):"; cin >> time;
cout << "\t\t\t" << principal << endl;
cout << "\t\t\t" << rate << endl;
cout << "\t\t\t" << time << endl;
cout << "\t\t\t************ MENU ********************" << endl;
cout << "\t\t\t1: Simple Interest" << endl;
cout << "\t\t\t2: Compound Interest" << endl;
cout << "\t\t\t3: Effective Annual Rate (Compound Interest)" << endl;
cout << "\t\t\t****************************************" << endl;
cout << "\t\t\tEnter You Choice:"; cin >> choice;
if(choice == 3)
{
cout << "\t\t\tNo of Period in a Year:";
cin >> period;
}
switch(choice)
{
case 1:
SI = simple_interest (principal, rate, time);
Amount = principal + SI;
cout << "\n\n\n";
cout << "\t\t\tSimple Interest =" << " "<< SI << endl;
cout << "\t\t\tTotal Amount =" << " " << Amount<< endl;
break;
case 2:
Amount = compound_interest(principal, rate, time);
CI = Amount - principal;
cout << "\n\n\n";
cout << "\t\t\tCompound Interest with Principal =" << " "
<<
Amount << endl;
cout << "\t\t\tTotal Compound Interest ="<< " " <<
CI << endl;
break;
case 3:
Amount = compound_interest_ear(principal, rate, time, period);
CI = Amount - principal;
EAR = pow((1 + (rate/period)),period)-1;
cout << "\n\n\n";
cout << "\t\t\tCompound Interest with Principal =" << " " <<
Amount << endl;
cout << "\t\t\tTotal Compound Interest =" << " " <<
CI << endl;
cout << "\t\t\tEffective Annual Rate =" << " " <<
EAR << endl;
cout << "\t\t\tNomianal Rate =" << " " <<
rate << endl;
break;
default:
cout << "\t\t\tSorry ! Try again";
break;
}
cout << "\n\n\n";
cout << "\t\t\tDo you want to Continue?" << endl;
cout << "\t\t\tEnter -99 to end" << endl;
cout << "\t\t\tOr Enter any other number to Continue:";
cin << choice;
}
system("PAUSE");
return EXIT_SUCCESS;
}
// Simple interest
double simple_interest(double principal, double rate, double time)
{
double SI = principal * rate * time;
double Amount = principal + SI;
return SI;
}
// Compound Interest
double compound_interest(double principal, double rate, double time)
{
double CI;
double Amount;
Amount = principal * pow((1 + rate),time);
return Amount;
}
// Compound Interest with the effective annual rate
double compound_interest_ear(double principal,double rate, double time,double period)
{
double CI, EAR, Amount;
period = period * time;
Amount = principal * pow((1 + (rate/period)),period);
return Amount;
}
Output – Interest Calculations
The output of the program to do interest calculations is given below.