Sonoma State University
Department of Computer Science
CS-460: Programming Languages
Exercise 15a

Objective:

To become familiar with the Perl scripting language by modifying parameters for a simple Perl script which calculates interest.

Instructions:

  • Modify the Perl script compound_interest.pl by changing the values for principal (starting amount in dollars), duration (in years), and apr (annual percentage rate for interest). Optional: modify compounding frequency. Then generate results.
compound_interest.pl
#!/usr/bin/perl -w

$principal = 10000; # $principal is our starting amount
$year = 2025; # beginning year when interest will begin compounding
$duration = 25; # How many years are we saving up?
$apr = 9; # This is our annual percentage rate.
$compounding = 4; # How often per year the interest is compounded (4 = quarterly)


$new_balance = $principal * (1 + $apr / 100 / $compounding) ** ($compounding * $duration);
print "Principal: $principal\n";
print "Number of years: $duration\n";
print "Interest rate: $apr%\n";
printf "New principal after $duration years: %10.2f\n",$new_balance;
print "\n";
print "YEAR PRINCIPAL INTEREST\n";
print "-----------------------------------\n";
printf "%4d %10.2f 0\n", $year, $principal;
for my $i (1 .. $duration)
{
  $year++;
  $new_balance = $principal * (1 + $apr / 100 / $compounding) ** ($compounding);
  printf "%4d %10.2f %10.2f\n", $year, $new_balance, $new_balance - $principal;
  $principal = $new_balance;
}

Preparation to make the BASH program executable:

  1. Once you've uploaded the program to Blue (blue.cs.sonoma.edu), be sure to enter the change mode command to make the program executable: chmod 750 compound_interest.pl

Submitting your solution

  • Please submit your modified BASH script for word_scramble.sh to Canvas.
  • Since this is a group assignment, please include the names of each member of your team along with "Exercise 15a" when submitting your solution.