C++ Program to Find Compound Interest

31/03/2024 0 By indiafreenotes

Calculating Compound interest is a pivotal concept in finance, illustrating how investments grow over time when interest is reinvested to earn additional interest. This principle is not only foundational in understanding financial growth but also serves as a practical example to explore and deepen one’s knowledge of programming in C++. Crafting a program to calculate compound interest involves utilizing variables, arithmetic operations, input/output mechanisms, and the mathematical library for functions like pow for exponentiation.

Understanding Compound Interest

Compound interest is calculated using the formula:

A = P (1+r / n​) ^ nt

where:

  • A is the amount of money accumulated after n years, including interest.
  • P is the principal amount (the initial sum of money).
  • r is the annual interest rate (decimal).
  • n is the number of times that interest is compounded per year.
  • t is the time the money is invested or borrowed for, in years.

Program Overview

Let’s develop a C++ program that prompts the user to enter the principal amount, annual interest rate, the number of times interest is compounded per year, and the number of years. The program will then calculate and display the amount of money accumulated after those years, including compound interest.

#include <iostream>

#include <cmath> // For pow function

using namespace std;

int main() {

    double principal, rate, amount;

    int compoundFrequency, time;

    // User input

    cout << “Enter the principal amount: “;

    cin >> principal;

    cout << “Enter the annual interest rate (in percentage): “;

    cin >> rate;

    cout << “Enter the number of times interest is compounded per year: “;

    cin >> compoundFrequency;

    cout << “Enter the number of years the money is invested: “;

    cin >> time;

    // Converting annual rate percentage to a decimal

    rate = rate / 100;

    // Compound interest formula

    amount = principal * pow((1 + rate / compoundFrequency), compoundFrequency * time);

    // Display the result

    cout << “The accumulated amount after ” << time << ” years is: ” << amount << endl;

    return 0;

}

 

Dissecting the Program

  • Including the Math Library

The program includes the <cmath> header, which provides access to the pow function, necessary for performing exponentiation in the compound interest formula.

  • Variables and Input

The program uses double for principal, rate, and amount to ensure precision in financial calculations. It uses int for compoundFrequency and time, as these are typically whole numbers.

  • Conversion and Calculation

The annual interest rate provided by the user is converted from a percentage to a decimal by dividing by 100. This is a crucial step, as the mathematical formula for compound interest requires the rate to be in decimal form.

The pow function is used to calculate the compound interest, effectively applying the formula to compute the future amount after interest is compounded over time.

  • Output

Finally, the program outputs the accumulated amount, demonstrating how much the initial principal grows over the specified period at the given interest rate and compounding frequency.