Friday, 23 February 2018

java - Turn annual interest into monthly



Here's the problem, I need to turn annual interest into monthly. My program is going to ask for an interest rate in the form of annual and calculate FV by compounding the annual interest rate monthly with monthly payments. For example 12% annual when compounded gives me an interest rate of ~ 0.948% monthly, only I can't figure out how to turn this equation into java code.



Here is my entire code, the output for the entered arguments is $3,829.95. This is the correct output only I had to find this interest rate by manually finding it, My question is what would be the correct formula to turn this into code when taking an annual interest rate and converting it to the same interest rate monthly, I have tried Math.pow((1 + r/12.0), 12); in place of multiplier and I don't get the expected results.(I apologize for my unorganized code, I am everywhere as I can't figure this out.)



import java.text.DecimalFormat;

public class It {
static double Investment;
static double months;
static double multiplier = 1 + .009488792935; //This is what I need to change.
public static void math(double Investment, double years )
{
It.Investment = Investment;
It.months = years * 12;
calculate();

}
public static void calculate()
{
double amount = 0;
for(int i = 0; i < months; i++)
{
amount = (amount + Investment) * multiplier;
}
DecimalFormat f = new DecimalFormat("$,###.00");
System.out.println("Amount: " + f.format(amount));
}

public static void main(String[] args)
{
math(300, 1);

}
}

Answer



I fixed your code, but I don't think I should post it here b/c this might be your homework.I should not take away your learning opportunity. Anyway, first, you created too many unnecessary variables and classes. With the code you presented, you can do it in one function. Second, you might want to do it with a loop b/c it's your homework requirement. Personally, I will avoid loop if I can do it simply by a line. In terms of how to do it by a loop, I think the answer below provided you a good guild line.



How to do it?



Simply, implement the below formula in java



Annual Compound Interest Formula:



FV = Investment * (1+r/n)^nt where r is your interest rate, n is the number of time it compounded, and t is the number of year.



and predefined your annual interest rate as global variable.



Note: There is a mistake in your result. Given 300 dollars as initial investment should not result $3,829.95 in one year. That's too good to be true.


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...