Perl: A compound interest calculator, Part II

Compound interest is probably studied more in high school than it was twenty years ago.  The tutor amends the perl program from September 10 to cover the general case of compound interest.

Back in my September 10 article, I offered a short Perl program that calculates compound interest with annual compounding. However, what about monthly, weekly, daily, or other compounding frequencies? Today, I’ll show the changes needed for the program to cover all possibilities.

For those wanting a refresher on the general formula for compound interest – or else on what compounding means – see my article here. For those curious about the ideas behind the Perl program in the September 10 article, search this blog for perl. Otherwise, click, on right pane, (or right here) the computer science category; you’ll find the perl articles in there.

The new program follows. The changes from the original are in lilac. Those changes enable the program to cover the general case – that is, where the compounding may be more than annually.


#!/usr/bin/perl
$principal =$ARGV[0];
$percent=$ARGV[1];
$rate=$ARGV[1]/100;
$time=$ARGV[2];
$ppyear=$ARGV[3];#compounding periods per year
$futurevalue=$principal*(1+$rate/$ppyear)**($time*$ppyear);
print “The principal amount is $principal\n”;
print “The annual interest rate is $percent percent\n”;
print “The compoundings per year is $ppyear\n”;

print “The time duration of the investment is $time years\n\n”;
print “The future value of the intestment is $futurevalue\n\n”;

Now, when you run this program from the command line, you’ll need to add in one last parameter: the compoundings per year. If the compounding is monthly, the number will be 12; weekly will mean 52.

Example: Imagine the new, improved compound interest calculator is called intcalc1.txt. Let’s further imagine you want to use it to calculate the future value of an investment of $2500, at interest rate 3.2%, compounded monthly, over a term of 5 years. What command would you enter at the terminal to accomplish that calculation?

Solution: Assuming you are in the proper directory (see my article here if you need a primer), you will enter the following:

perl intcalc1.txt 2500 3.2 5 12

If all goes well, you might be greeted with the following response:


The principal amount is 2500
The annual interest rate is 3.2 percent
The compoundings per year is 12
The time duration of the investment is 5 years

The future value of the investment is 2933.152481…

While there is more to say about this program, today’s purpose is satisfied. I’ll be extending the discussion in future posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tagged with: , ,

Leave a Reply