Compound Intrest Calculation

Compound Interest Calculation

Make a program with a function named calculateValueAfterInterest. calculateValueAfterInterest determines the final value of an amount after applying a monthly interest rate over a specific number of months. The function will accept three parameters: the initial value, the monthly interest rate, and the number of months.

Keep in mind that the compound interest is calculated monthly in this case.

Have the user input the inital value, montly interest rate (as a percentage), and the number of months.

Answer

#include <iostream>
#include <cmath>

double calculateValueAfterInterest(double initialValue, double monthlyRate, int months)
{
    double monthlyRateDecimal = monthlyRate / 100.0; // Convert percentage to decimal
    return initialValue * pow(1 + monthlyRateDecimal, months);
}

int main() 
{
    double initialValue, monthlyRate;
    int months;
    std::cout << "Enter the initial value: ";
    std::cin >> initialValue;
    std::cout << "Enter the monthly interest rate (as a percentage, e.g., 5 for 5%): ";
    std::cin >> monthlyRate;
    std::cout << "Enter the number of months: ";
    std::cin >> months;

    double finalValue = calculateValueAfterInterest(initialValue, monthlyRate, months);
    std::cout << "Value after " << months << " months: " << finalValue << "\n";
}

This program uses the pow function to get the monthly rate. <cmath> is required to use pow.

Alternative function that uses loops instead of pow

double calculateValueAfterInterest(double initialValue, double monthlyRate, int months)
{
    double monthlyRateDecimal = monthlyRate / 100.0; // Convert percentage to decimal
    for (int i = 0; i < months; i++) 
    {
        initialValue += initialValue * monthlyRateDecimal;
    }
    return initialValue;
}
Questions