C programs to calculate compound interest have been shown here. Both of the iterative and recursive approaches have been covered. Compound interest can also be calculated using formula:
$CI = {\Large p*(1 + \frac{r}{n})^{nt} - p}$
Here $CI$ represents compound interest. Pricipal amount, interest rate, periodicity of payment and time in years have been represented by $p$, $r$, $n$ and $t$ respectively. As an example, If $p = 5000$, $r = 7.5$ , $n = 4$ and $t = 2$ then by using the above formula, we get the value of compound interest $CI = 801.11$.
The following section covers the C programs to calculate compound interest along with algorithm, pseudocode and time complexity of the program.
Page content(s):
5. Program: Iterative approach
6. Program: Recursive approach
Additional content(s):
1. Algorithm to calculate compound interest
1. Take input of Pricipal amount ($p$), interest rate ($r$), payment periodicity ($n$) and years ($t$)
2. Calculate compound interest using formula $CI = p*(1 + \frac{r}{n})^{nt} - p$
3. Return the computed value $CI$
2. Pseudocode to calculate compound interest
Input: Pricipal $p$, interest rate $r$, payment periodicity $n$, time in years $t$
Output: Compound interest $ci$
1. Procedure compoundInterest($p$, $r$, $n$, $t$):
2.
3.
4. End Procedure
3. Time complexity to calculate compound interest
Time Complexity: O(nt)
Here $t$ is the no. of years and $n$ is the payment periodicity.
4. C Program & output to calculate compound interest using formula
/******************************* alphabetacoder.com C program to calculate compound interest using formula ********************************/ #include <stdio.h> #include <math.h> 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 printf("Enter the principal balance = "); scanf("%f", & p); printf("Enter the annual interest rate = "); scanf("%f", & r); printf("Enter compound frequency / year = "); scanf("%d", & n); printf("Enter the year = "); scanf("%f", & t); // calculate compounded value // using formula amount = p * pow((1 + r / (100 * n)), n * t); //find the compound interest ci = amount - p; // display result rounded up to two decimal places printf("Compound interest = %0.2f", ci); return 0; }
Output
Enter the principal balance = 20000
Enter the annual interest rate = 8
Enter compound frequency / year = 4
Enter the year = 10
Compound interest = 24160.76
5. C Program & output to calculate compound interest using Iteration
/******************************* alphabetacoder.com C program to calculate compound interest using Iteration *********************************/ #include <stdio.h> int main() { // declare variables float ci, amount, p, r, t; int n, i; // take input of initial principal amount, // interest rate, periodicity of payment and year printf("Enter the principal balance = "); scanf("%f", & p); printf("Enter the annual interest rate = "); scanf("%f", & r); printf("Enter compound frequency / year = "); scanf("%d", & n); printf("Enter the year = "); scanf("%f", & 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 rounded up to two decimal places printf("Compound interest = %0.2f", ci); return 0; }
Output
Enter the principal balance = 33000
Enter the annual interest rate = 8.25
Enter compound frequency / year = 2
Enter the year = 10
Compound interest = 41065.21
6. C Program & output to calculate compound interest using recursion
/******************************* alphabetacoder.com C program to calculate compound interest using recursion ********************************/ #include <stdio.h> // 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, i, t; // take input of initial principal amount, // interest rate, periodicity of payment and year printf("Enter the principal balance = "); scanf("%f", & p); printf("Enter the annual interest rate = "); scanf("%f", & r); printf("Enter compound frequency / year = "); scanf("%d", & n); printf("Enter the year = "); scanf("%d", & t); // call function to compute compound interest ci = compound_interest(p, p, r, n, t, 0); // display result rounded up to two decimal places printf("Compound interest = %0.2f", ci); return 0; }
Output
Enter the principal balance = 33000
Enter the annual interest rate = 8.25
Enter compound frequency / year = 2
Enter the year = 10
Compound interest = 41065.21