Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to find compound interest in c without using pow function

+1 vote
asked Oct 23, 2021 by Diego Lim (130 points)

1 Answer

+1 vote
answered Oct 24, 2021 by Peter Minarik (88,130 points)
edited Oct 25, 2021 by Peter Minarik

Let's see if I get you right.

You would like to invest startAmount at a rate of interestRate and you'd like to know how much money you'll have at the end of n investment periods.

In this case, you'd have the following formula: totalMoney = startAmount * (1 + interestRate)n

, where interestRate is expressed as a floating-point number (e.g. 10% --> 0.10)

Now, you ask how you can do this without raising anything to a certain power.

Well, if you take the basic definition of the ab, it means that you multiply a with itself b times.

In programming terms, instead of using the pow() function, you can write a loop that multiplies a given number of times. E.g.:

float CalculateInterest(float startAmount, float interestRate, size_t n)
    float multiplier = 1.0 + interestRate;
    for (size_t i = 0; i < n; i++)
        startAmount *= multiplier;
    return startAmount;

Is this what you're after?

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.