**C++ programs to calculate compound interest** have been shown here. Both of the iterative and recursive approaches have been covered. Computing compound interest using formula has also been shown here.

The following section covers the C++ programs to calculate compound interest.

#### Page content(s):

**2. Program: Iterative approach**

**3. Program: Recursive approach**

#### Additional content(s):

## 1. C++ Program & output to calculate compound interest using formula

/*************************************************************** alphabetacoder.com C++ program to calculate compound interest using formula ****************************************************************/ #include <iostream> #include <cmath> using namespace std; int main(){ // declare variables float ci, amount, p, r, t; int n; // take input of initial principal amount, // interest rate, periodicity of payment and year cout<<"Enter the principal balance = "; cin>> p; cout<<"Enter the annual interest rate = "; cin>> r; cout<<"Enter compound frequency / year = "; cin>> n; cout<<"Enter the year = "; cin>> t; // calculate compounded value // using formula amount = p * pow((1 + r/(100 * n)), n*t); //find the compound interest ci = amount - p; // display result cout<<"Compound interest = "<<ci<<endl; return 0; }

Output

Enter the principal balance = 17000

Enter the annual interest rate = 10

Enter compound frequency / year = 1

Enter the year = 2

Compound interest = 3570

## 2. C++ Program & output to calculate compound interest using loop (Iteration)

/*************************************************************** alphabetacoder.com C++ program to calculate compound interest using loop (Iteration) ****************************************************************/ #include <iostream> using namespace std; int main(){ // declare variables float ci, amount, p, r, t; int n, i; // take input of initial principal amount, // interest rate, compound frequency and year cout<<"Enter the principal balance = "; cin>> p; cout<<"Enter the annual interest rate = "; cin>> r; cout<<"Enter compound frequency / year = "; cin>> n; cout<<"Enter the year = "; cin>> t; // initialize amount = p; // calculate compounded value using loop for(i = 0; i < n * t; i++) amount = amount + amount * (r / (n * 100)); //find the compound interest ci = amount - p; // display result cout<<"Compound interest = "<<ci<<endl; return 0; }

Output

Enter the principal balance = 5000

Enter the annual interest rate = 7.5

Enter compound frequency / year = 4

Enter the year = 6

Compound interest = 2808.96

## 3. C++ Program & output to calculate compound interest using recursion

/*************************************************************** alphabetacoder.com C++ program to calculate compound interest using recursion ****************************************************************/ #include <iostream> using namespace std; // recursive function to compute compound interest float compound_interest(float p0, float p, float r, int n, int t, int itr){ // if number of total no of compound reached if(itr == n * t) return (p - p0); // calculate interest float interest = p * (r / (n * 100)); // call function return compound_interest(p0, p + interest, r, n, t, itr + 1); } int main(){ // declare variables float ci, p, r; int n, t; // take input of initial principal amount, // interest rate, compound frequency and year cout<<"Enter the principal balance = "; cin>> p; cout<<"Enter the annual interest rate = "; cin>> r; cout<<"Enter compound frequency / year = "; cin>> n; cout<<"Enter the year = "; cin>> t; // call function to compute compound interest ci = compound_interest(p, p, r, n, t, 0); // display result cout<<"Compound interest = "<<ci<<endl; return 0; }

Output

Enter the principal balance = 2700

Enter the annual interest rate = 5

Enter compound frequency / year = 4

Enter the year = 10

Compound interest = 1737.77

## No comments:

## Post a Comment

If you have any doubts or suggestions, please leave a note.