Open a text editor, for example, NotePad, and enter the following lines of code:
#!/usr/bin/perl -w
print “Monthly deposit: “; # 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:
- ‘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.
More Stories
Protein Shake Eating plan
Top Word Games for iPhone and iPad
Consumer Experience (UX) Design Vs Consumer Interface (UI) Style