Perl programming: a compound interest calculator, part I
The tutor continues his coverage of programming with Perl. Today, we’ll look at some math operators.
Over the summer, I covered beginners’ aspects of Perl programming. Searching this blog for perl, or else choosing the computer science category on the right pane, should point you towards the articles.
Back in February 2013, I covered compound interest. Today’s article continues in that direction as well, with a Perl programming example.
The following program calculates the future value of an investment. The user provides the principal amount, the interest rate, and the time duration when calling the program from the command line.
#!/usr/bin/perl
$principal =$ARGV[0];
$percent=$ARGV[1];
$rate=$ARGV[1]/100;
$time=$ARGV[2];
$futurevalue=$principal*(1+$rate)**$time;
print “The principal amount is $principal\n”;
print “The annual interest rate is $percent percent\n”;
print “The time duration of the investment is $time\n\n”;
print “The future value of the intestment is $futurevalue\n\n”;
Let’s imagine you name the program intcalc.txt, and that you want to use it to calculate the future value of an investment with
principal=$4000
rate=3.5%
time=21 years
Navigating (in the terminal) to the directory that contains intcalc.txt, you might call it from the command line as follows:
perl intcalc.txt 4000 3.5 21
Notice the Perl division operator /. Of course, * means “multiply”, while ** means “exponent.”
As is, this program considers only annual compounding. In a future post, we’ll amend it to accommodate other possibilities.
Cheers:)
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.