CS173: Intro to Computer Science - Introduction to Iteration (3 Points)
Developed by Professor Tralie and Professor Mongan.Exercise Goals
The goals of this exercise are:- To use iteration to compute a discrete value
Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is wmongan
(non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit).
Netid |
Driver.java
public class Driver {
public static void main(String[] args) {
// balance = principal * totalInterestRate
// totalInterestRate = totalInterestRate * compoundFactor, computed n*t times
// ... where compoundFactor = (1 + (rate / n))
// ... where t = periods (years), n = compoundTimesPerPeriod (times per year),
// ... and rate = annual interest rate (2% == 0.02)
int principal = 1; // 1 dollar invested
double interest = 1; // at 100% interest
int years = 1; // for 1 year
/* TODO: Change this to compound daily */
int compoundTimesPerYear = 12; // compounded monthly
double compoundFactor = 1 + (interest / compoundTimesPerYear);
double totalInterestRate = compoundFactor;
// iterate one fewer time because we initialized the variable above, which counts as one multiplication
for(/* TODO: Fill this in */) {
totalInterestRate = totalInterestRate * compoundFactor;
}
double finalBalance = principal * totalInterestRate;
System.out.println(finalBalance);
System.out.println(totalInterestRate);
}
}
Driver.main(null);
Program Output
Fun With Limits
As your compoundTimesPerYear
variable increases, your total balance approaches a certain famous constant: what is it?