I'm trying to make a program that will allow the user to determine different aspects when considering a loan. The first equation is supposed to determine what the monthly payment would be given the loan amount, interest rate, and number of months. The second equation is supposed to determine how many payments(or months) one would have to make given the loan amount, interest rate, and monthly payment. This is the code I'm testing with, but I can't seem to get valid output.
float amount = 20000;
float rate = (float) 7.5;
float months = 60;
float payment = 450;
float answer = (float) (((amount*(rate/1200))*(1+(Math.pow((rate/1200), months))))/((1+Math.pow((rate/1200), months))-1));
System.out.println(answer);
float answer2 = (log(payment/amount)-log((payment/amount)-(rate/1200)))/(log(1+(rate/1200)));
System.out.println(answer2);
For the first equation, I first kept getting errors because it wanted the answer to be a double and kept telling me that for my rate variable I couldn't convert from double to float, so I had to put in casts. Once I got the errors to go away, I keep getting infinity for an answer.
For the second equation, it keeps saying I can't divide by zero. Does anybody know any solutions to this. When looking online, it seems as though I'm formulated my equations correctly, so I don't know how to make them work.
Also, if anyone would happen to know a good formula for determining the interest rate, that would be so helpful.