C++ Program to Calculate Compound Interest

Howdy readers, today you will learn how to write a C++ program to calculate compound interest.

Compound interest is an interest accumulated on the principal and interest together over a given time period.

The formula for calculating the compound interest is as follows:

CI = P(1 + r/n)nt – P

Where,

  • P is the principal amount
  • r is the rate of interest
  • t is the overall tenure
  • n is the frequency or no. of times the interest is compounded annually

If the given principal is compounded annually, the amount after the time period at percent rate of interest, r, is given as:

A = P(1 + r/100)t, and C.I. would be: P(1 + r/100)t – P

The below program prompts the user to enter the principal amount, rate of interest and time period. Then, it computes the compound interest using the formula: CI = P(1 + r/100)t – P.

So, without any delay, let’s begin this tutorial.

C++ Program to Calculate Compound Interest

C++ Program

// C++ Program to Calculate Compound Interest
#include <iostream>
#include <math.h>
using namespace std;

int main(){
    float principal, time, rate, CI, amount;
    
    // Taking input
    cout << "Enter the principal amount: ";
    cin >> principal;
    cout << "Enter the rate of interest: ";
    cin >> rate;
    cout << "Enter the time period: ";
    cin >> time;
    
    // Calculate compound interest
    amount = principal * pow((1 + rate/100), time);
    CI = amount - principal;
    
    // Display result
    cout << "Compound interest is: " << CI << endl;
    
    return 0;
}

Output

Enter the principal amount: 50000
Enter the rate of interest: 4
Enter the time period: 5
Compound interest is: 10832.6
C++ Program to Calculate Compound Interest

Explanation

    float principal, time, rate, CI, amount;

In this program, we have declared five float data type variables named principal, time, rate, CI and amount.

    cout << "Enter the principal amount: ";
    cin >> principal;
    cout << "Enter the rate of interest: ";
    cin >> rate;
    cout << "Enter the time period: ";
    cin >> time;

Then, the user is asked to enter the principal amount, rate of interest and time period. The entered values get stored in the principal, rate and time named variables respectively.

    amount = principal * pow((1 + rate/100), time);
    CI = amount - principal;

Now, compound interest is calculated using the formula, CI = P(1 + r/100)t – P. The compound interest gets stored in the CI named variable.

The pow() function returns the result of the first argument raised to the power of the second argument. For example: pow(a, b) = ab.

   cout << "Compound interest is: " << CI << endl;

At last, the compound interest is printed on the screen using the cout statement.

Conclusion

Today you learned how to write a C++ program to calculate compound interest.

If you have any questions regarding the tutorial, then comment down your queries in the comment section.

Thanks for reading.

Happy Coding!!

Also Read:

Leave a Comment