Previous | Table of Contents | Next |
Exercise: Interest CalculatorFor this exercise, you're going to perform a compound interest calculation. This program will calculate the interest on a savings account, given some information about interest rates, deposits, and time. The formula you're going to use is as follows: Using your text editor, type the program from Listing 2.2 and save it as Interest. Do not type in the line numbers. Make the program executable according to the instructions you learned in Hour 1. When you're done, try running the program by typing the following at a command line: perl Interest Listing 2.3 shows a sample of the Interest program's output. Listing 2.2. The Complete Source of the Interest Program1: #!/usr/bin/perl -w 2: 3: print "Monthly deposit amount? "; 4: $pmt=<STDIN>; 5: chomp $pmt; 6: 7: print "Annual Interest rate? (ex. 7% is .07) "; 8: $interest=<STDIN>; 9: chomp $interest; 10: 11: print "Number of months to deposit? "; 12: $mons=<STDIN>; 13: chomp $mons; 14: 15: # Formula requires a monthly interest 16: $interest/=12; 17: 18: $total=$pmt * ( ( ( 1 + $interest) ** $mons ) -1 )/ $interest; 19: 20: print "After $mons months, at $interest monthly you\n"; 21: print "will have $total.\n";
Listing 2.3. Output from the Interest Program1: Monthly deposit amount? 180 2: Annual Interest rate? (ex. 6% is .06) .06 3: Number of months to deposit? 120 4: After 120 months, at 0.005 monthly you 5: will have 29498.2824251624. |
Previous | Table of Contents | Next |