C++ Program for Compound Interest Calculations

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.

  1. Simple Interest
  2. Compound Interest
  3. Effective Annual Rate (Compound Interest that compounded many times during the year).

The program requires three input given below.

  1. Principal amount
  2. Rate in Percent
  3. Time in years
  4. Period, 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.
[cc lang=”c” tab_size=”4″]
A = (P + SI) = (P + Prt) => P (1 + rt)
[/cc]
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) = 1000 * 150/100 = $1500.

SI = A – P

SI = 1500 – 1000 = $500.
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 the there is a new principal amount.

The compound interest is computed using the following formula
[cc lang=”cpp” tab_size=”4″]
A = P (1 + r)^t
[/cc]
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)3 = 2000(1.2)3 = 2000 * 1.728 = $3456

CI = 3456 – 2000 = $1456

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.
[cc lang=”cpp”]A = P (1 + (r/n))^nt[/cc]
Where

A = Total Compounded amount with 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
[cc lang=”cpp” tab_size=”4″]EAR = (1 + (r/n))^n – 1[/cc]
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)) 12*3

= 1000(1 + 0.0083)36

= 1000 (1.0083)36

= $1348.18

CI = 1348.18 – 1000

= $348.18

EAR = (1 + (0.10/12)) 12 – 1]

= (1 + 0.0083)12 – 1

= 0.1046

= 10.46%

Flowchart - Interest Calculation Program
Flowchart – Interest Calculation Program

 

Program Code – Interest Calculation Program

[cc lang=”cpp” tab_size=”4″]
#include

#include

#include

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 interestdouble simple_interest(double principal, double rate, double time){ double SI = principal * rate * time; double Amount = principal + SI; return SI;}// Compound Interestdouble compound_interest(double principal, double rate, double time){ double CI; double Amount; Amount = principal * pow((1 + rate),time); return Amount;}// Compound Interest with effective annual ratedouble 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;}[/cc]

Output – Interest Calculations

The output of the program to do interest calculations is given below.

Output - Simple Interest
Output – Simple Interest

 

Output - Compound Interest
Output – Compound Interest

 

 

Advertisements