JDT

 

FREE Downloads
Articles
Tutorials

 
Google

Create a Compound Interest Calculator in Perl


In this tutorial we will look at how to use Perl to create a simple compound interest calculator.

Creating the script

Enter the following lines of code into a text editor:

#!/usr/bin/perl -w

print "Monthly deposit: "; # prompt for input
$deposit=<STDIN>; # get input from keyboard
chomp $deposit; # remove the newline character from the end of the variable

print "Interest rate (3, 4, 5.5, etc): "; # prompt for input
$interest=<STDIN>; # get input from keyboard
chomp $interest; # remove the newline character from the end of the variable

# Change interest from 3, 4, 5, etc to .03, .04, .05, etc
$interest=$interest*.01;

# Change interest to a monthly multiplier
$interest=$interest/12;

print "No of months: "; # prompt for input
$nMonths=<STDIN>; # get input from keyboard
chomp $nMonths; # remove the newline character from the end of the variable

# The interest calculation
$total=$deposit * (((1 + $interest) ** $nMonths) -1 ) / $interest;

print "After $nMonths months you will have a total amount of $total\n";

Save the script as interest.pl. Make a note of the directory/folder in which you have saved it.

Running the script

You need to run the script from a command line prompt, so open a terminal window/MS-DOS prompt. Change to the directory/folder where the interest.pl file is located, and type the following command:

perl interest.pl

When prompted, enter the monthly deposit, interest rate, and the number of months during which the money is deposited.

Error messages

If the script did not work, you probably received one of the following error messages:

  • 'Bad command or filename' or 'command not found'. This means that Perl has not been added to the PATH variable. See your operating system help/documentation for information on how to fix this problem.
  • 'Can't open perl script interest.pl: A file or directory does not exist'. This probably means that you are not in the folder/directory where you saved the script, in which case you should change to the correct location.
  • If you get a syntax error, it probably means you have mis-typed the contents of the file. Open the file and fix any mistakes.

Use an online version now

An online version of a compound interest calculator, written in PHP (not Perl), is available by clicking the following link.

Compound interest calculator


Author: Backrubber
John Dixon Technology Ltd







Go back to Perl Tutorials home page

Go back to Tutorials home page



Earnings Tracker is John Dixon Technology's FREE open source accounting / bookkeeping software tool.

Earnings Tracker is aimed at contractors and freelancers, and enables you to perform many bookkeeping and accounting tasks, helping you to keep track of your company's earnings and outgoings.

The software is written in PHP and MySQL and is available to use for FREE online, or as a FREE download.

As it is written in open source software, you are free to modify the code yourself, enabling you to produce a customized version of Earnings Tracker that fits your own specification.

Earnings Tracker can also be used simply as a dividend, corporation tax, or VAT calculator.

Need free accounting software
 



JDT

Copyright Notice for John Dixon Technology Ltd

Privacy Statement

Terms & Conditions