Compound Interest Calculations

You would think that simple interest (from last week’s Lesson) would be enough. After all, if you’re investing, you make money; if you’re paying back a loan, you can figure the math. That aspect of interest wasn’t enough for mathematical and banking geniuses, who devised a second type of interest calculation, which is “interest on the interest.”

Yes, compound interest sounds evil. Compounds aren’t readily associated with nice things, but the word “compound” is also a verb, to compound.

The notion is this: If you get paid 3 percent interest on your investment, shouldn’t that interest also earn interest? During the year, as the interest accumulates, it’s added to the base or principal amount. That sounds like fun!

Calculating compound interest has been a topic in mathematics for centuries. Rather than bother with the detailed specifics or boring history, Figure 1 details the formula for compound interest, along with the common variable names used in math, spreadsheets, and other calculations.

Figure 1. Compound Interest formula.

If you’re a math nerd, you might find it interesting to see how the formula was concocted. Otherwise, long-story-short: Because the interest is applied to itself, you need an exponential operator. That’s how the formula works.

You duty as a programmer is to convert the compound interest formula into the C language, which I’ve done in the following code:

#include <stdio.h>
#include <math.h>

int main()
{
    int i,n,t;
    float p,fv,r;

    puts("Compound Interest Calculator");

    /* gather data */
    printf("Initial balance: $");
    scanf("%f",&p);
    printf("Interest rate percentage: ");
    scanf("%d",&i);
    r = (float)i/100;
    printf("Compunding times per year: ");
    scanf("%d",&n);
    printf("Term in years: ");
    scanf("%d",&t);

    /* calculate */
    fv = p * pow( ( 1 + (r/n) ), t*n );
    printf("Future value is %.2f\n",fv);

    return(0);
}

This code gathers input from the user similar to the simple interest calculator introduced for last week’s Lesson. Here I’m using shorter variable names to match the formula presented in Figure 1.

The calculation is made at Line 23. The pow() function requires the math.h header (Line 2). It handles the parentheses part of the formula, which packs into the function’s first argument. The t*n exponent is the second argument.

Here’s a sample run:

Compound Interest Calculator
Initial balance: $1000
Interest rate percentage: 5
Compunding times per year: 1
Term in years: 10
Future value is 1628.89

If you increase the number of compounding periods to monthly, you can eek out a bit more income:

Compound Interest Calculator
Initial balance: $1000
Interest rate percentage: 5
Compunding times per year: 12
Term in years: 10
Future value is 1647.02

Eventually, as the number of compounding periods tends to infinity, the value returned flattens out. This effect is related to a mathematical constant, Euler’s number e. I’ll explore that calculation in next week’s Lesson.

Leave a Reply