I am supposed to make a monthly payment calculator in java with the given formula.
The formula for me to use is
M = P * i/ 1 - (1+i)^-n
where
P is the loan principal (i.e. the amount borrowed)
i is monthly interest rate (annual_interest_rate / 12; expressed as decimal)
N is time (number of monthly payments in total years of loan; i.e. years * 12)
The code below is my attempted function to get the monthly payment.
But if I put in 6 years with a loan amount of 200, I get 140 using the formula.
I am stumped as to why I get that number. Any help would be appreciated
public static int calMonthlyPay(double loanAmt, int y) {
double m = 0.0, interest = 0.0, annualIRate = 0.0;
double months = 0.0;
months = y * 12;
annualIRate = getAnnualIRate(y);
interest = annualIRate / 12;
System.out.println(interest);
System.out.println(months);
System.out.println(loanAmt);
System.out.println(y);
m = (loanAmt * (interest - Math.pow((1 + interest), -months))); // This is my formula calculation
System.out.println(m);
return 0;
}
private static double getAnnualIRate(int y) {
switch (y) {
case 2:
return 5.7;
case 3:
return 6.2;
case 4:
return 6.8;
case 5:
return 7.5;
case 6:
return 8.4;
default:
return 8.4;
}
}
If I understood your formula right, it should be:
m = loanAmt * interest - Math.pow(1 + interest, -months);
What you have now is :
m = (loanAmt * (interest / 1 - Math.pow((1 + interest), -months))) =
(loanAmt * (interest - Math.pow((1 + interest), -months)))
You should use the parentheses correctly :
m = loanAmt * (interest / (1 - Math.pow(1 + interest, -months)));
Your formula is incorrect, it need to be
loanAmt * (interest / (1 - Math.pow (1 + interest, -months)));
And there's a error in interest value formula, it need to be
interest = annualIRate / 100 / 12;
So your method calMonthlyPay(200, 6) gives 3 now, which is correct.
Related
I am trying the write the formula in the image using Java. However, I get the wrong output. This is what I am so far for the computation.
Assume Input is: Loan Amount = 50000 and Length = 3 year.
My output is currently "676.08" for 'Monthly Payment' and "25661.0" for "Total Interest Payment".
"Monthly payment" should be "1764.03" and "Total Interest Payment" should be "13505.05".
Code is:
//Program that displays the interest rate and monthly payment based on the loan amount and length of loan the user inputs.
import java.util.Scanner;
public class bankLoanSelection{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//Declarations.
double interestRate;
final double n = 12;
//Generates an account number in the range of 1 to 1000.
int accountnumber = (int)(Math.random() * 1000)+1;
//User input
System.out.print("Enter Loan Amount: $");
double L = input.nextDouble();
System.out.print("Length of Loan (years): ");
double i = input.nextDouble();
//selection structure using if and if-else statements.
if (L <= 1000)
interestRate = .0825;
else if (L <= 5000)
interestRate = .0935;
else if (L <= 10000)
interestRate = .1045;
else interestRate = .1625;
//Computations.
double MonthlyPayment = L * (interestRate / n) * Math.pow(1 + interestRate / n, i * n) / Math.pow(1 + interestRate / n, i * n) - 1;
double TotalAmountOfLoan = MonthlyPayment * i * n;
double TotalInterest = L - TotalAmountOfLoan;
//Output.
System.out.println("Account Number: #" + accountnumber);
System.out.println("Loan Amount: $" + L);
System.out.println("Loan Length (years): " + i);
System.out.println("Interest Rate: " + interestRate * 100 + "%");
System.out.printf("Total Monthly Payment: $%.2f%n", MonthlyPayment); //Rounds second decimal
System.out.println("Total Interest Payments: $" + TotalInterest);
}
}
You have to put round parentheses between condition.
It should be:
double MonthlyPayment = L * ((interestRate / n) * Math.pow(1 + interestRate / n, i * n))
/ (Math.pow(1 + interestRate / n, i * n) - 1);
You need to correct the arrangement of brackets in the denominator, the correct formula would be:
double numerator = L * (interestRate / n) * Math.pow(1 + interestRate / n, i * n)
double denominator = Math.pow(1 + interestRate / n, i * n) - 1;
double MonthlyPayment = numerator / denominator;
Try to break the problem into smaller units to solve.
I am trying to calculate monthly loan payments the formula Loan Payment = Amount / Discount Factor on www.thebalance.com
According to the website discount factor is calculated with this formula (D) = {[(1 + i) ^n] - 1} / [i(1 + i)^n] I tried interpreting this into Java and came up with
double discountFactor = (Math.pow((1 + interest), numberOfPayments) - 1) / Math.pow(interest * (1 + interest), numberOfPayments);
But it outputs infinityam not so good with Math, can someone help point out the issue?
double discountFactor = (Math.pow((1 + interest), numberOfPayments) - 1) /
(interest * Math.pow((1 + interest), numberOfPayments));
interest is not part of the power, it should be outside power.
put the 'interest' outside the second power function not inside.
double discountFactor = (Math.pow((1 + interest),numberOfPayments)-1)/(interest*Math.pow((1+interest),numberOfPayments));
The formula to compute the monthly payment is as follows:
monthlyPayment = (loanAmount x monthlyInterestRate) / (1 – (1 / (1 + monthlyInterestRate)numberOfYears x 12 ))
In the above formula, you have to compute (1 + monthlyInterestRate)numberOfYears x 12 ). The pow(a,b) method in the Java API Math class can be used to compute ab.
so how to put this in pow(a,b) method ?
The javadoc for the java.lang.Math class might help you here. Is there something specific you don't understand about that?
So the code to compute the b power of a should look something like this:
double result = Math.pow(a, b);
The formula you have does not look quite right. From Exact_formula_for_monthly_payment The formula should be
P= (L i)/(1- 1/(1+i)^n )
Where L is the loan amount, i is the monthly interest, and n number of periods. In your formula
monthlyPayment = (loanAmount x monthlyInterestRate) / (1 – (1 / (1 + monthlyInterestRate)numberOfYears x 12 ))
the exponential sign has been missed out. I think you want
monthlyPayment = (loanAmount x monthlyInterestRate) / (1 – (1 / (1 + monthlyInterestRate) ^ (numberOfYears x 12) ))
To calculate this in java you would want
monthlyPayment = (loanAmount * monthlyInterestRate) /
(1 – (1 / Math.pow(1 + monthlyInterestRate,numberOfYears * 12)));
Which could be simplified to
monthlyPayment = (loanAmount * monthlyInterestRate) /
(1 – Math.pow(1 + monthlyInterestRate,-numberOfYears * 12));
using a negative exponent.
I am trying to create a program that calculates the futureValue that is in the following formula
futureValue = investmentAmount × (1 + monthlyInterestRate)^years×12
It is a visual program and it calculates the futureValue.Variables: investmentAmount monthlyInterestRate and years are taken from textfields.Here my code is like this
public void Calculator(){
double x = (Double.parseDouble(AnnualInterestRate.getText())/12) + 1;
double y = Double.parseDouble(Years.getText()) * 12;
double mult = Math.pow(x, y);
double futureValue = Double.parseDouble(InvesmentAmount.getText()) * mult;
lblNewLabel_3.setText("$" + String.format("%.2f",futureValue));
}
for these values:
InvesmentAmount : 1000
Years : 2
AnnualInterestRate : 6.5
futureValue should be 1,138.43 but futureValue that is calculated by my program is 32491635,80
I can't see the mistake and It would be great if anybody could help.
If your user is supposed to be inputting the interest rate as a percentage, you need to divide it by 100 as part of your calculation. E.g.
double x = (Double.parseDouble(AnnualInterestRate.getText())/1200) + 1;
Output: $1138.43
am not getting this program to display my instalments correctly can I please get some help thanks......
package Loops;
import java.util.Scanner;
/**
*
*
*/
public class program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//variabled decleared
double rate;
double payment;
//input
System.out.print("Enter Loan Amount:");
double principal = input.nextDouble();
System.out.print("Enter Annual Interest:");
double interest = input.nextDouble();
System.out.print("Total payments per year:");//12=monthly,4= quartely,2=semi-annually and 1=annually
double period = input.nextDouble();
System.out.print("Enter Loan Length :");
int length = input.nextInt();
//proces
double n = period * length;
rate = interest / 100;
double monthly_rate = rate / period;
payment = principal * (principal * (monthly_rate * Math.pow((1 + monthly_rate), n)));
System.out.printf("Your Monthly sum is %.2f", payment);
}
}
principal = 50000; //Redacted. Eating my words.
period = 4;
length = 4;
n = 16;
rate = 0.085;
monthly_rate = 0.085 / 16 = 0.0053125;
payment = 50000 * 50000 * 0.0053125 * (1 + 0.0053125) ^ 16;
= 2.5x10^9 * 0.0053125 * 1.088;
= Something remainingly massive
Basically... your formula is wrong. Wouldn't you need to divide by the power quotient? Where is your source on that formula?
payment = principal * (rate + (rate / ( Math.pow(1 + rate, n) - 1) ) );
Source
Example:
payment = 50000*(0.085+(0.085/(1.085^16-1)))
= 5830.68
Try this for your formula:
//this is how much a monthly payment is
payment = (rate + (rate / ((Math.pow(1 + rate), n) -1)) * principal
This is based off one of the first google results for the formula. Please post the results and expected answer if it is wrong.
I'm pretty sure your formula is just off, as you stated above that there should be a denominator in the equation.
You can use r* Math.pow ((1+r),n) to calculate the numerator and part of the denominator