Of Great Interest

I’m frustrated, as are many investors, at the lack of interest generated in “savings” accounts these days. It’s pitiful. The interest formula is based on solid mathematics, which is easy to code in C. Doing so may help you understand the concept of interest and how it works.

First, some definitions.

Interest is money paid on a debt or money received from an investment. To be upbeat, I’m going to frame this discussion on money received from an investment: You put $100 in the bank and every month your statement shows a little bit extra added. That’s interest.

Interest has two types: simple and compound. These two terms fold into the second topic, which is how the interest is calculated. Simple interest is the easy one to figure:

( interest_rate * initial_balance * time_periods ) / payment_frequency

Say the interest rate is 3 percent, which would be generous right now. The initial balance is $100, held for 12 months. The payment frequency is annually, or once every 12 months:

( 0.03 * 100 * 12 ) / 12 = 3

It’s important to remember that “3 percent” is 0.03 and not 3.0

An investment of $100, held for a year at a simple interest rate of 3 percent paid annually, yields $3 at the end of the term; your initial $100 investment would be worth $103.

When the interest payment is made monthly,the payment_frequency variable changes to 1:

( 0.03 * 100 * 12 ) / 1 = 36

The time_periods variable, or term, is still 12 months, one year. With this change, after a year at 3 percent interest paid monthly, the yield is $36; you end up with $136. Yes, that’s a tremendous investment, and it’s why most interest is calculated annually and paid monthly.

The following code is a simple interest calculator. It fetches the desired values from standard input and generates a result.

#include <stdio.h>

int main()
{
    float initial_balance,interest_rate,interest_earned;
    int interest_input,payment_frequency,time_periods;

    puts("Simple Interest Calculator");

    /* gather data */
    printf("Enter the initial balance: $");
    scanf("%f",&initial_balance);
    printf("Enter the interest rate percentage: ");
    scanf("%d",&interest_input);
    interest_rate = (float)interest_input/100;
    printf("Enter the payment frequency in months: ");
    scanf("%d",&payment_frequency);
    printf("Enter the term of the loan in months: ");
    scanf("%d",&time_periods);

    /* calculate */
    interest_earned =
        (interest_rate * initial_balance * time_periods)
        /payment_frequency;

    /* display results */
    printf("The ending balance will be $%.2f\n",
            initial_balance+interest_earned);

    return(0);
}

Data is gathered in Lines 10 through 19. At Line 13, the interest rate is prompted as a percent and an int value fetched. Getting input here is an issue in many interest-calculating programs, because you don’t know whether the user will type 3 or 0.03 for “3 percent.” The code assumes integer input, and Line 15 makes the conversion to a float value.

The simple interest is calculated in a single statement that spans Lines 22 through 24. The long variable names make the code readable, but the statement long. The split format (made possible because C ignores the white space) looks more like the traditional simple interest formula.

The result is displayed at Line 27.

Here is a sample run:

Simple Interest Calculator
Enter the initial balance: $100
Enter the interest rate percentage: 3
Enter the payment frequency in months: 12
Enter the term of the loan in months: 12
The ending balance will be $103.00

And:

Simple Interest Calculator
Enter the initial balance: $100
Enter the interest rate percentage: 3
Enter the payment frequency in months: 1
Enter the term of the loan in months: 12
The ending balance will be $136.00

Remember, these are simple interest calculations. The second type of interest is compound interest, which is a bit more involved and confusing to calculate. I’ll put off that topic to next week’s Lesson.

Leave a Reply