Create a Compound Interest Calculator in Perl

Create a Compound Interest Calculator in Perl

 

 

Open a text editor, for example, NotePad, and enter the following lines of code:

#!/usr/bin/perl -w

print “Monthly deposit:  https://www.mobills.com.br/calculadoras/calculadora-juros-compostos/ “; # prompt for input
$deposit=; # 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=; # 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=; # 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”;

(Note: the spaces on either side of STDIN are for display purposes only in this article. In your script, you can omit them.)

Save the script as interest.pl. Make a note of the directory/folder where 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 that the money is deposited.

Error messages

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

 

Leave a Reply

Your email address will not be published.