Compound Interest) If an amount a is invested at an interest rate r compounded n
ID: 3916548 • Letter: C
Question
Compound Interest) If an amount a is invested at an interest rate r compounded n times per year, then the final value f at the end of one year is given by f = a(1 + r/n) n. This is the familiar formula for compound interest (so if we compound annually, then n = 1). Typically, compounding is done quarterly (n = 4), daily (n = 365) or continuously (n ??). In a class called CompoundInterest, write a method that implements the compound interest formula f above. This method should take the following parameters: a double a (as in the given formula), a double r (as in the given formula), and an int n (as in the given formula). This method should return the value of the investment at the end of a year (as specified by the formula f). You should NOT use any Math library functions or any imports (hint: you will need a loop). Test your method in main using an initial investment of $100.00, an interest rate of 5 percent (i.e. r = 0.05), and the following values for the compound number: 1, 4, 365, 1,000, and 10,000.
language: Java
Explanation / Answer
public class CompoundInterest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Hard coding the values; you can use scanner function if you wish to get the values from user
double principle = 100, rate = 0.05, years = 2, n=2;
//Calling the compound function to calculate the total amount
double amount = compound(principle,rate, years, n);
//Printing out the results
System.out.println("Compound Interest: "+(amount-principle));
System.out.println("Total Amount: "+amount);
}
public static double compound(double principle, double rate, double years, double n){
double amount = principle;
//using for loop instead of math.pow function to calculate
for(int i=0;i<(years*n); i++){
double w = amount * (1+rate/n);
amount = w;
}
return amount;
}
}
Output:
Compound Interest: 10.38128906249996
Total Amount: 110.38128906249996
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.